Practice
1. You are given a binary tree and need to count nodes that are "good" -- nodes whose value is greater than or equal to all values on the path from the root to that node. Which algorithmic approach guarantees an optimal solution for this problem?
easy
Solution
Step 1: Understand the problem requirement
The problem requires comparing each node's value to all values on the path from the root, not just its parent.Step 2: Identify the approach that tracks path state
Only DFS or BFS that carries the maximum value seen so far on the path can correctly determine if a node is "good".Final Answer:
Option B -> Option BQuick Check:
Tracking max value on path ensures correct counting [OK]
Hint: Track max value on path, not just parent [OK]
Common Mistakes:
- Only compare node to parent, missing path max
- Trying DP without path state
- Sorting nodes loses tree structure
2. You are given a perfect binary tree where each node has two children. The task is to connect each node's
next pointer to its immediate right neighbor on the same level, or NULL if there is none. Which approach guarantees an optimal solution with O(1) extra space and O(n) time complexity?easy
Solution
Step 1: Understand the problem constraints
The tree is perfect, so every node has two children, and all leaves are at the same level.Step 2: Identify the approach that uses constant extra space
Usingnextpointers already set on the current level to connect the next level avoids extra data structures like queues.Final Answer:
Option A -> Option AQuick Check:
Optimal approach uses existing pointers to traverse levels [OK]
Hint: Use next pointers to traverse levels without extra space [OK]
Common Mistakes:
- Assuming BFS with queue is optimal space-wise
- Confusing DFS recursion with constant space
- Using DP which is unrelated
3. Identify the bug in the following near-correct implementation of the optimal zigzag level order traversal:
def zigzagLevelOrder(root):
if not root:
return []
currentLevel = [root]
nextLevel = []
result = []
left_to_right = True
while currentLevel:
level_vals = []
while currentLevel:
node = currentLevel.pop()
level_vals.append(node.val)
if left_to_right:
if node.left:
nextLevel.append(node.left)
if node.right:
nextLevel.append(node.right)
else:
if node.right:
nextLevel.append(node.right)
if node.left:
nextLevel.appen(node.left) # typo here
result.append(level_vals)
currentLevel, nextLevel = nextLevel, []
left_to_right = not left_to_right
return result
medium
Solution
Step 1: Locate suspicious line
The line 'nextLevel.appen(node.left)' contains a typo: 'appen' instead of 'append'.Step 2: Understand impact
This typo causes a runtime AttributeError, breaking the code during execution.Final Answer:
Option B -> Option BQuick Check:
Typo in method name -> runtime error [OK]
Hint: Check method names carefully for typos [OK]
Common Mistakes:
- Forgetting to reverse levels
- Popping from wrong end
- Misplacing direction toggle
4. What is the time complexity of the optimal iterative BFS approach using two queues for N-ary tree level order traversal, where n is the number of nodes in the tree?
medium
Solution
Step 1: Identify operations per node
Each node is enqueued and dequeued exactly once, and its children are iterated once.Step 2: Sum over all nodes
Since total nodes are n, total operations scale linearly with n.Final Answer:
Option A -> Option AQuick Check:
Traversal visits each node once -> O(n) time [OK]
Hint: BFS visits each node once, no repeated work [OK]
Common Mistakes:
- Assuming children are enqueued multiple times causing O(n²)
5. Suppose the problem is extended so that node values can be negative and the tree can contain duplicate values. Which modification to the iterative DFS approach is necessary to correctly count good nodes?
hard
Solution
Step 1: Consider negative and duplicate values impact
Negative values do not affect logic if max_val is tracked per path; duplicates are allowed as long as comparison is >=.Step 2: Confirm max_val update timing
Updating max_val before pushing children ensures correct max_val state per path, no need to initialize differently.Final Answer:
Option D -> Option DQuick Check:
Maintaining max_val per path handles negatives and duplicates correctly [OK]
Hint: Keep max_val updated per path, no special init needed [OK]
Common Mistakes:
- Initializing max_val incorrectly
- Switching traversal unnecessarily
- Skipping duplicates incorrectly
