Practice
1. You are given two arrays representing the preorder and inorder traversal sequences of a binary tree. Which approach guarantees reconstructing the original tree efficiently without redundant subtree searches?
easy
Solution
Step 1: Understand the problem constraints
Reconstructing a binary tree from preorder and inorder requires knowing root positions quickly to split subtrees.Step 2: Identify the optimal approach
Using a hash map for inorder indices allows O(1) root lookups, avoiding repeated O(n) searches in recursion.Final Answer:
Option D -> Option DQuick Check:
Hash map lookup avoids repeated scanning [OK]
Hint: Hash map lookup avoids repeated inorder searches [OK]
Common Mistakes:
- Assuming greedy or BFS can reconstruct tree correctly
- Confusing DP with tree construction
- Ignoring the need for quick root index lookup
2. Consider the following iterative DFS code for finding all root-to-leaf paths with a given sum. Given the tree below and targetSum = 7, what is the final returned list?
Tree structure:
5
/ \
4 8
/ / \
11 13 4
Target sum: 7
def pathSum(root, targetSum):
if not root:
return []
res = []
stack = [(root, [root.val], root.val)]
while stack:
node, path, current_sum = stack.pop()
if not node.left and not node.right:
if current_sum == targetSum:
res.append(path)
if node.right:
stack.append((node.right, path + [node.right.val], current_sum + node.right.val))
if node.left:
stack.append((node.left, path + [node.left.val], current_sum + node.left.val))
return res
easy
Solution
Step 1: Trace paths and sums
Paths: 5->4->11 sum=20, 5->8->13 sum=26, 5->8->4 sum=17; none equals 7.Step 2: Confirm no leaf path sums to 7
Since no leaf path sums to 7, result list remains empty.Final Answer:
Option A -> Option AQuick Check:
No leaf path sums to 7, so empty list returned [OK]
Hint: Check sums only at leaf nodes [OK]
Common Mistakes:
- Confusing intermediate node sums with leaf sums
- Forgetting to check leaf condition
3. You need to convert a binary tree into a string and then reconstruct the exact same tree from that string. Which approach guarantees that the tree structure, including null children, is preserved and can be reconstructed efficiently?
easy
Solution
Step 1: Understand the need for null markers
Without null markers, the tree structure cannot be uniquely reconstructed because missing children are ambiguous.Step 2: Recognize BFS with null markers preserves structure
Level order traversal with null markers records nodes level by level, capturing the exact shape of the tree, enabling correct deserialization.Final Answer:
Option A -> Option AQuick Check:
Level order with null markers uniquely encodes tree structure [OK]
Hint: Null markers are essential to preserve tree shape [OK]
Common Mistakes:
- Ignoring null markers leads to ambiguous deserialization
- Assuming inorder traversal alone can reconstruct arbitrary trees
- Using greedy or DP approaches that don't preserve structure
4. The following code attempts to find the Lowest Common Ancestor using parent pointers. Identify the line containing the subtle bug that causes incorrect results when one node is ancestor of the other.
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
medium
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]
Hint: Parent map construction loop must fully process both nodes [OK]
Common Mistakes:
- Assuming parent map always complete
- Ignoring incomplete parent map causing KeyError
- Misplacing bug in ancestor set loops
5. Suppose the binary tree nodes can have negative values and you want to find the diameter defined as the longest path in terms of number of edges (ignoring node values). Which modification to the standard diameter algorithm is necessary to handle this variant correctly?
hard
Solution
Step 1: Recognize diameter depends on path length (edges), not node values.
Negative values do not affect height or path length calculations.Step 2: Confirm standard height-based diameter algorithm works unchanged.
Heights and diameter calculations rely on structure, so no changes are needed.Final Answer:
Option A -> Option AQuick Check:
Diameter depends on edges, not node values [OK]
Hint: Diameter is structural, unaffected by node values [OK]
Common Mistakes:
- Confusing diameter with max sum path
- Ignoring negative nodes incorrectly
- Switching to BFS unnecessarily
