Practice
Solution
Step 1: Understand traversal order requirement
Inorder traversal visits left subtree, then node, then right subtree, matching the problem's order.Step 2: Identify approach with O(1) space
Morris traversal achieves inorder traversal without recursion or stack by temporarily modifying tree pointers, restoring them after use.Final Answer:
Option C -> Option CQuick Check:
Morris traversal is known for O(1) space inorder traversal [OK]
- Confusing preorder with inorder traversal order
- Assuming BFS can produce inorder sequence
- Thinking DP applies to traversal order
depth after the second iteration of the while loop?
Tree structure:
- Root node 1
- Left child 2
- Right child 3
- Left child of 2 is 4
from collections import deque
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def maxDepth(root):
if root is None:
return 0
queue = deque([root])
depth = 0
while queue:
level_size = len(queue)
for _ in range(level_size):
node = queue.popleft()
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
depth += 1
return depth
root = TreeNode(1, TreeNode(2, TreeNode(4)), TreeNode(3))
print(maxDepth(root))
Solution
Step 1: Trace first iteration of while loop
Initially, queue contains root (1). level_size=1. After processing node 1, enqueue its children 2 and 3. Increment depth to 1.Step 2: Trace second iteration of while loop
Queue now has nodes 2 and 3. level_size=2. Process node 2 (enqueue 4), process node 3 (no children). Increment depth to 2.Final Answer:
Option B -> Option BQuick Check:
Depth increments after each level processed; after second level depth=2 [OK]
- Counting nodes instead of levels
- Off-by-one in depth increment
- Ignoring children enqueueing
Solution
Step 1: Understand the problem constraints
The problem requires checking if any root-to-leaf path sums to the target. This naturally fits a tree traversal pattern.Step 2: Identify the best approach
DFS with early stopping is optimal because it explores paths deeply and stops as soon as a valid path is found, avoiding unnecessary work.Final Answer:
Option A -> Option AQuick Check:
DFS explores paths fully and stops early when possible [OK]
- Believing greedy approach works for sums
- Confusing BFS with DFS for path sums
Solution
Step 1: Understand diameter definition counts edges, not nodes.
The diameter is the number of edges on the longest path, so it should be left + right, not left + right + 1.Step 2: Identify the bug line.
Line 9 adds 1 incorrectly, causing diameter to be off by one.Final Answer:
Option B -> Option BQuick Check:
Diameter must be sum of left and right heights (edges), not nodes [OK]
- Adding 1 to diameter sum
- Not using nonlocal for diameter
- Returning wrong height value
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
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]
- Assuming parent map always complete
- Ignoring incomplete parent map causing KeyError
- Misplacing bug in ancestor set loops
