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
Solution
Step 1: Understand problem constraints and path definition
The path can start and end anywhere but must go downwards, so checking all paths naively is expensive.Step 2: Recognize prefix sum with DFS pattern
Using a prefix sum hash map during DFS allows counting all valid paths in O(n) time by tracking cumulative sums and their frequencies.Final Answer:
Option C -> Option CQuick Check:
Prefix sum + DFS solves in linear time [OK]
- Assuming paths must start at root or leaf
- Using brute force DFS from every node without pruning
- Trying greedy pruning which fails on negative values
Solution
Step 1: Understand the problem constraints
The problem requires checking if any root-to-leaf path sums to the target. This naturally fits a tree traversal pattern.Step 2: Identify the best approach
DFS with early stopping is optimal because it explores paths deeply and stops as soon as a valid path is found, avoiding unnecessary work.Final Answer:
Option A -> Option AQuick Check:
DFS explores paths fully and stops early when possible [OK]
- Believing greedy approach works for sums
- Confusing BFS with DFS for path sums
Solution
Step 1: Understand the impact of allowing node reuse
Allowing cycles means paths can revisit nodes infinitely, so naive DFS or postorder traversal fails due to infinite loops.Step 2: Identify correct approach
Dynamic programming with cycle detection and memoization prevents infinite recursion and correctly computes max sums with repeated nodes.Final Answer:
Option B -> Option BQuick Check:
Memoization with cycle detection handles repeated nodes safely [OK]
- Using same postorder traversal causes infinite loops
- Bellman-Ford is for shortest paths, not max sums with negative cycles
- DFS without cycle checks leads to infinite recursion
Solution
Step 1: Understand impact of negative values
Negative values mean partial sums can decrease later, so pruning based on current sum is unsafe.Step 2: Adjust algorithm accordingly
Early stopping based on partial sums must be removed to avoid missing valid paths.Final Answer:
Option D -> Option DQuick Check:
Negative values break pruning assumptions, so early stopping must be disabled [OK]
- Assuming early stopping always works
- Switching to BFS unnecessarily
