Practice
def postorderTraversal(root):
result = []
stack = []
last_visited = None
current = root
while current or stack:
while current:
stack.append(current)
current = current.left
peek_node = stack[-1]
if peek_node.right and last_visited != peek_node.right:
current = peek_node.right
else:
result.append(peek_node.val)
last_visited = stack.pop()
return result
Solution
Step 1: Trace traversal on given tree
Start at root(1), go left (None), push 1. Then peek 1, right child is 2, move to 2, push 2, go left to 3, push 3, left None.Step 2: Process nodes in postorder
3 has no children, append 3. Back to 2, right visited, append 2. Back to 1, right visited, append 1.Final Answer:
Option C -> Option CQuick Check:
Output matches postorder [3, 2, 1] [OK]
- Confusing order of appending nodes
- Off-by-one in stack popping
def postorderTraversal(root):
result = []
stack = []
last_visited = None
current = root
while current or stack:
while current:
stack.append(current)
current = current.left
peek_node = stack[-1]
if peek_node.right and last_visited == peek_node.right:
current = peek_node.right
else:
result.append(peek_node.val)
last_visited = stack.pop()
return result
What is the bug in this code?Solution
Step 1: Examine condition for traversing right subtree
Correct logic requires moving to right child if it exists and was not visited yet, so condition must be last_visited != peek_node.right.Step 2: Identify effect of wrong condition
Using last_visited == peek_node.right causes skipping right subtree traversal or infinite loops.Final Answer:
Option B -> Option BQuick Check:
Fixing condition restores correct postorder traversal [OK]
- Using == instead of !=
- Not updating last_visited
- Appending too early
Solution
Step 1: Identify inorder_index initialization
inorder_index should start at the last index (len(inorder) - 1) because postorder is processed from end to start.Step 2: Consequences of wrong initialization
Starting at 0 causes incorrect comparisons and popping logic, leading to wrong tree structure or runtime errors.Final Answer:
Option B -> Option BQuick Check:
Correct inorder_index initialization is critical for stack popping logic [OK]
- Wrong inorder_index initialization
- Confusing postorder traversal direction
- Incorrect stack popping conditions
Solution
Step 1: Understand rob_current calculation
rob_current should be node.val plus the not_rob values of left and right children, because robbing current node forbids robbing immediate children.Step 2: Identify the bug
Line 5 incorrectly adds left[0] and right[0] (rob values of children), violating adjacency constraint.Final Answer:
Option A -> Option AQuick Check:
Using rob values of children overcounts and breaks correctness [OK]
- Mixing rob and not_rob indices in tuple
- Forgetting adjacency constraints
class Solution:
def sumNumbers(self, root: TreeNode) -> int:
self.total = 0
def dfs(node, current_number):
if not node:
return
current_number = current_number * 10 + node.val
self.total += current_number # Bug here
if not node.left and not node.right:
return
dfs(node.left, current_number)
dfs(node.right, current_number)
dfs(root, 0)
return self.total
Solution
Step 1: Understand when sums should be added
Sum should only include complete root-to-leaf numbers, so addition must happen only at leaf nodes.Step 2: Identify incorrect addition
Adding current_number at every node (including non-leaf) causes partial numbers to be summed, inflating total incorrectly.Final Answer:
Option B -> Option BQuick Check:
Sum only at leaves fixes the bug [OK]
- Adding partial path sums at non-leaf nodes
- Not resetting total between calls
