Practice
def rightSideView(root: Optional[TreeNode]) -> List[int]:
rightside = []
def dfs(node, depth):
if not node:
return
if depth == len(rightside):
rightside.append(node.val)
dfs(node.left, depth + 1) # Line X
dfs(node.right, depth + 1) # Line Y
dfs(root, 0)
return rightside
Which line contains the bug causing incorrect right side view?Solution
Step 1: Understand traversal order
The code visits left child before right child, so left nodes are appended first at each depth.Step 2: Why this causes bug
Visiting left first causes left nodes to be recorded as visible, overwriting right nodes that should be visible from the right side.Final Answer:
Option B -> Option BQuick Check:
Swapping left and right child calls fixes the bug [OK]
- Visiting left child before right in DFS causes wrong nodes appended
- Appending nodes without checking depth length
- Not handling null nodes properly
Solution
Step 1: Identify that each node is processed once
Each node is enqueued once and dequeued once, so total operations proportional to n.Step 2: Confirm no nested loops over nodes beyond level size
Inner loop runs for each level's nodes, summing to n overall, so total time is O(n).Final Answer:
Option C -> Option CQuick Check:
Each node visited once -> O(n) time [OK]
- Confusing level width w as multiplicative factor
- Assuming sorting or heap operations per level
- Ignoring that inner loop sums to n over all levels
Solution
Step 1: Identify enqueue order for mirrored nodes
Mirrored nodes must be enqueued as (t1.left, t2.right) and (t1.right, t2.left) to compare symmetric positions.Step 2: Locate incorrect enqueue causing wrong pairs
The code enqueues (t1.left, t2.left) and (t1.right, t2.right), which compares left subtree to left subtree, breaking symmetry checks.Final Answer:
Option C -> Option CQuick Check:
Incorrect enqueue order causes false positives on asymmetric trees [OK]
- Swapping mirrored enqueue order
- Missing null checks causing exceptions
- Incorrect value comparisons
Solution
Step 1: Understand BFS level order traversal processes nodes per level
We can track two largest values inline by comparing each node's value to current top two during the level loop.Step 2: Avoid sorting or multiple traversals for efficiency
Sorting all values or multiple BFS runs increase time complexity unnecessarily; inline tracking keeps O(n) time.Step 3: Confirm DFS with heaps adds extra space and complexity
DFS with heaps is more complex and less efficient than inline BFS tracking.Final Answer:
Option A -> Option AQuick Check:
Inline top-2 tracking per level -> O(n) time, O(w) space [OK]
- Sorting all values per level causing O(n log n) time
- Multiple BFS passes increasing runtime
- Using DFS with heaps unnecessarily complicates solution
Solution
Step 1: Understand the new requirement
We want the minimum depth to a leaf node with a specific value, not just any leaf.Step 2: Modify BFS to check leaf value before returning
BFS should stop only when it finds a leaf node whose value matches the target; otherwise, continue searching.Final Answer:
Option A -> Option AQuick Check:
BFS guarantees shortest path; checking leaf value before stopping is correct [OK]
- Stopping at first leaf regardless of value
- Pruning BFS incorrectly by node values
- Switching to DFS losing BFS optimality
