Practice
Solution
Step 1: Trace stack operations and count updates
Start with stack=[(3,3)]. Pop (3,3), count=1. Push right (4,3), left (1,3). Pop (1,3), node.val=1 < 3 no count. Push left (3,3). Pop (3,3), node.val=3 >=3 count=2. Pop (4,3), node.val=4 >=3 count=3. Push right (5,4), left (1,4). Pop (1,4), node.val=1 <4 no count. Pop (5,4), node.val=5 >=4 count=4.Step 2: Final count after traversal
Count is 4 after visiting all nodes.Final Answer:
Option A -> Option AQuick Check:
Count matches expected good nodes in example [OK]
- Off-by-one counting
- Ignoring max_val update before pushing children
- Mixing left and right child order
1
/ \
2 3
/ \
4 5
After calling connect(root), what is the value of root.left.right.next?
class Node:
def __init__(self, val=0, left=None, right=None, next=None):
self.val = val
self.left = left
self.right = right
self.next = next
def connect(root):
if not root:
return None
if root.left and root.right:
root.left.next = root.right
next_node = root.next
while next_node:
if next_node.left:
next_child = next_node.left
break
if next_node.right:
next_child = next_node.right
break
next_node = next_node.next
else:
next_child = None
if root.right:
root.right.next = next_child
elif root.left:
root.left.next = next_child
connect(root.right)
connect(root.left)
return root
Solution
Step 1: Trace connections at root (node 1)
root.left (2) next -> root.right (3). root.right (3) next -> null (no next at root level).Step 2: Trace connections at node 2
Node 2 has left child 4, no right child. Its next is 3, which has right child 5. So 4.next -> 5.Final Answer:
Option B -> Option BQuick Check:
4.next points to 5, not null or 3 [OK]
- Assuming 4.next is null because 2 has no right child
- Confusing next pointers with parent pointers
- Thinking next points to parent or sibling's parent
Solution
Step 1: Identify BFS traversal cost
BFS visits each node once, enqueuing and dequeuing each node exactly one time.Step 2: Analyze per-node operations
Each node's children are checked once, so total operations scale linearly with n.Final Answer:
Option C -> Option CQuick Check:
Linear traversal over all nodes confirms O(n) time [OK]
- Confusing BFS with DFS recursion stack cost
- Assuming repeated level processing causes O(n^2)
- Mistaking height for total nodes
Solution
Step 1: Identify number of nodes processed
The BFS approach processes each node exactly once, enqueuing pairs of nodes representing mirrored positions.Step 2: Understand why O(n^2) is incorrect
There is no nested iteration over all nodes; each node is visited once, so complexity is linear, not quadratic.Final Answer:
Option A -> Option AQuick Check:
Linear traversal of all nodes with pairwise checks yields O(n) time [OK]
- Assuming pairwise comparisons multiply to O(n^2)
- Confusing recursion stack space with BFS space
- Thinking repeated subtree checks cause log factor
Solution
Step 1: Understand symmetry in multi-child trees
Symmetry means the first child on the left subtree mirrors the last child on the right subtree, second mirrors second last, etc.Step 2: Modify BFS to enqueue children in mirrored order
Enqueue children pairs from opposite ends of the children list to compare mirrored positions correctly.Final Answer:
Option B -> Option BQuick Check:
Mirrored enqueue order for multiple children preserves symmetry checks [OK]
- Enqueuing children in normal order causing false positives
- Assuming BFS cannot be adapted for multi-child trees
- Ignoring deeper levels of the tree
