Practice
1. You need to visit all nodes of a binary tree in the order: root node first, then recursively the left subtree, followed by the right subtree. Which algorithmic approach guarantees this traversal order efficiently without extra space for recursion or stack?
easy
Solution
Step 1: Understand traversal order requirement
The problem requires visiting root before left and right subtrees, which is preorder traversal.Step 2: Identify approach with no extra space
Morris preorder traversal uses threaded binary trees to achieve preorder traversal without recursion or stack, thus optimal in space.Final Answer:
Option C -> Option CQuick Check:
Preorder visits root first; Morris traversal achieves this with O(1) space [OK]
Hint: Preorder visits root before children [OK]
Common Mistakes:
- Confusing inorder or postorder with preorder traversal
- Assuming recursion is always needed
- Using level-order which visits nodes by depth
2. You are given a binary tree and need to find the longest path from the root node down to the farthest leaf node. Which algorithmic approach guarantees an optimal solution to determine this maximum depth?
easy
Solution
Step 1: Understand the problem requires exploring all paths from root to leaves
Finding maximum depth means checking every path from root to leaves, so partial or greedy approaches won't guarantee correctness.Step 2: Identify algorithms that explore all nodes
DFS or BFS traverse all nodes systematically, ensuring the maximum depth is found. Greedy or sorting approaches do not consider all paths.Final Answer:
Option C -> Option CQuick Check:
DFS and BFS both visit all nodes to find max depth [OK]
Hint: Max depth requires full traversal, not greedy or sorting [OK]
Common Mistakes:
- Assuming greedy traversal finds max depth
- Confusing max depth with max node value
- Thinking sorting nodes helps depth calculation
3. Consider the following buggy code snippet for building a tree from inorder and postorder traversals. Which line contains the subtle bug that can cause incorrect tree construction or runtime errors?
medium
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]
Hint: inorder_index must start at last index, not zero [OK]
Common Mistakes:
- Wrong inorder_index initialization
- Confusing postorder traversal direction
- Incorrect stack popping conditions
4. Suppose the problem is modified so that node values can be negative, and you want to find all root-to-leaf paths summing to targetSum. Which modification to the DFS approach is necessary to ensure correctness?
hard
Solution
Step 1: Understand effect of negative values
Negative values can decrease current_sum, so pruning based on exceeding targetSum can miss valid paths.Step 2: Adjust DFS to remove pruning
To ensure all valid paths are found, pruning must be removed and all paths fully explored.Final Answer:
Option A -> Option AQuick Check:
Pruning breaks correctness with negative values; full exploration needed [OK]
Hint: Negative values invalidate pruning based on sum limits [OK]
Common Mistakes:
- Applying pruning blindly
- Switching to BFS unnecessarily
5. Suppose the problem is modified so that node values can be negative integers, and the goal remains to sum all root-to-leaf numbers formed by concatenation (including negative signs). Which of the following changes to the algorithm is necessary to handle this correctly?
hard
Solution
Step 1: Understand impact of negative values
Integer arithmetic with multiplication by 10 assumes digits 0-9; negative signs break this assumption.Step 2: Identify correct approach
Concatenating node values as strings preserves negative signs and digit order; convert to integer only at leaf nodes.Step 3: Confirm other options fail
Ignoring signs or skipping negatives leads to incorrect sums or incomplete paths.Final Answer:
Option D -> Option DQuick Check:
String concatenation handles negative digits correctly [OK]
Hint: Use string concatenation for negative digits [OK]
Common Mistakes:
- Assuming all digits are positive
- Using arithmetic without sign handling
