Practice
Solution
Step 1: Calculate tree height
Height h = 3 (nodes 1 -> 2 -> 4).Step 2: Binary search on last level nodes
Last level has indices 0 to 3. Check existence: - idx=0 exists (node 4) - idx=1 does not exist So left ends at 1.Final Answer:
Option A -> Option AQuick Check:
Count = (2^(3-1)-1) + left = 3 + 1 = 4 [OK]
- Off-by-one in binary search bounds
- Miscounting last level nodes
rob_current when dfs is called on the root node (value 3)?Solution
Step 1: Trace dfs on left child (value 2)
dfs(2) calls dfs(null) and dfs(3). dfs(null) returns (0,0). dfs(3) returns (3,0) because it has no children. So rob_current=3+0+0=3, not_rob_current=max(0,0)+max(0,0)=0. dfs(3) returns (3,0). For node 2: rob_current=2+0+0=2, not_rob_current=max(0,3)+max(0,0)=3. dfs(2) returns (2,3).Step 2: Trace dfs on right child (value 3)
dfs(3) calls dfs(null) and dfs(1). dfs(1) returns (1,0). So rob_current=3+0+0=3, not_rob_current=max(0,0)+max(1,0)=1. dfs(3) returns (3,1).Step 3: Calculate rob_current at root (value 3)
rob_current = 3 + left[1] + right[1] = 3 + 3 + 1 = 7.Final Answer:
Option A -> Option AQuick Check:
Sum matches known example output 7 [OK]
- Mixing rob and not_rob values for children
- Off-by-one in recursion returns
Solution
Step 1: Identify missing operation
Inside the if block, after creating node.left, the new node is not pushed onto the stack.Step 2: Consequences of missing stack append
Without pushing, the algorithm loses track of the left subtree root, causing incorrect tree or infinite loops.Final Answer:
Option B -> Option BQuick Check:
Stack must track all nodes to correctly build tree [OK]
- Forgetting to push left child nodes
- Incorrectly incrementing inorder_index
- Misinitializing root or stack
def lowestCommonAncestor(root, p, q):
parent = {root: None}
stack = [root]
while p not in parent or q not in parent:
node = stack.pop()
if node.left:
parent[node.left] = node
stack.append(node.left)
if node.right:
parent[node.right] = node
stack.append(node.right)
ancestors = set()
while p:
ancestors.add(p)
p = parent[p]
while q not in ancestors:
q = parent[q]
return q
Solution
Step 1: Analyze parent map construction loop
The loop 'while p not in parent or q not in parent:' ensures both nodes are in the parent map before ancestor sets are built.Step 2: Identify subtle bug
If one node is ancestor of the other, the loop may terminate prematurely if only one node is in parent map, causing KeyError later.Step 3: Explanation
The bug is that the loop condition does not guarantee both nodes are fully processed in the parent map before ancestor traversal.Final Answer:
Option A -> Option AQuick Check:
Loop condition must ensure both nodes are in parent map to avoid errors [OK]
- Assuming parent map always complete
- Ignoring incomplete parent map causing KeyError
- Misplacing bug in ancestor set loops
Solution
Step 1: Understand traversal constraints
Without left/right child pointers, Morris traversal is not applicable since it relies on child links.Step 2: Use parent pointers to simulate traversal
By tracking current and previously visited nodes, we can move up or down the tree to simulate inorder traversal iteratively.Final Answer:
Option D -> Option DQuick Check:
Tracking previous node enables correct traversal without extra space [OK]
- Trying to create threads on parent pointers
- Using recursion without child pointers
- Confusing BFS with inorder traversal
