Practice
result after processing the input tree [3,9,20,null,null,15,7] (3 is root, 9 left child, 20 right child, 15 and 7 are children of 20)?
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
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.append(node.left)
result.append(level_vals)
currentLevel, nextLevel = nextLevel, []
left_to_right = not left_to_right
return result
Solution
Step 1: Trace first level
Start with root 3, left_to_right is True, so level_vals = [3]. Children added to nextLevel: left 9, right 20.Step 2: Trace second level
Now currentLevel = [9, 20], left_to_right is False, pop 20 first, then 9. level_vals = [20, 9]. For 20, add right child 7 then left child 15 to nextLevel.Step 3: Trace third level
currentLevel = [7, 15], left_to_right is True, pop 15 then 7, level_vals = [15, 7]. No children to add.Final Answer:
Option A -> Option AQuick Check:
Order matches zigzag pattern for given tree [OK]
- Swapping children order incorrectly
- Mixing up left_to_right flag
from collections import deque
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def findBottomLeftValue(self, root: TreeNode) -> int:
queue = deque([root])
node = None
while queue:
node = queue.popleft()
if node.right:
queue.append(node.right)
if node.left:
queue.append(node.left)
return node.val
root = TreeNode(1, TreeNode(2), TreeNode(3, TreeNode(4), TreeNode(5)))
sol = Solution()
print(sol.findBottomLeftValue(root))
Solution
Step 1: Trace queue operations
Start with queue=[1]. Pop 1, enqueue right=3 then left=2 -> queue=[3,2]. Pop 3, enqueue right=5 then left=4 -> queue=[2,5,4]. Pop 2 (no children) -> queue=[5,4]. Pop 5 (no children) -> queue=[4]. Pop 4 (no children) -> queue=[]. Last popped node is 4.Step 2: Identify last popped node value
Last popped node is 4, so return 4.Final Answer:
Option C -> Option CQuick Check:
Last node popped is 4, confirming BFS with right-to-left insertion finds bottom-left node [OK]
- Confusing order of enqueueing children
- Assuming left child is processed before right in BFS
- Returning first node at last level instead of last popped
Solution
Step 1: Understand the problem requirement
The problem asks for the minimum depth to a leaf node, which means the shortest path from root to any leaf.Step 2: Identify the algorithm that finds shortest path in unweighted trees
BFS explores nodes level by level and stops immediately when it finds the first leaf, ensuring the shortest path is found efficiently.Final Answer:
Option A -> Option AQuick Check:
BFS stops at first leaf -> minimal depth found [OK]
- Assuming DFS always finds minimum depth efficiently
- Confusing greedy value-based choice with shortest path
- Thinking DP is needed for simple tree traversal
Solution
Step 1: Understand problem extension
Nodes can appear multiple times if they share column and row but differ by parent, so row and parent info must be tracked.Step 2: Evaluate modifications
Tracking (node, column, row) and sorting by parent ID ensures correct ordering. Ignoring row or parent loses correctness. Visited set prevents duplicates but may skip valid repeats.Final Answer:
Option A -> Option AQuick Check:
Tracking row and parent allows correct ordering of duplicates [OK]
- Ignoring row or parent info leads to incorrect grouping
- Using visited set that skips valid repeated nodes
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
