Practice
depth when the first leaf node is dequeued?
Tree structure:
- Root (1)
- Left child (2)
- Right child (3)
- Left child (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 minDepth(root):
if not root:
return 0
queue = deque([(root, 1)])
while queue:
node, depth = queue.popleft()
if not node.left and not node.right:
return depth
if node.left:
queue.append((node.left, depth + 1))
if node.right:
queue.append((node.right, depth + 1))
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3, TreeNode(4))
print(minDepth(root))
Solution
Step 1: Trace the BFS queue operations
Initially queue = [(root(1), 1)]. Dequeue root: not leaf, enqueue left(2,2) and right(3,2).Step 2: Dequeue next node and check leaf
Dequeue (2,2). Node 2 has no children, so it is a leaf. Return depth = 2.Final Answer:
Option D -> Option DQuick Check:
First leaf found at depth 2 -> correct BFS behavior [OK]
- Returning depth 1 because root is counted incorrectly
- Continuing BFS past first leaf node
- Confusing node values with depth
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: Check for empty tree handling
Code initializes queue with root without checking if root is None, causing queue to contain None.Step 2: Consequence of missing check
When root is None, popleft() returns None, and accessing node.val causes an error.Final Answer:
Option A -> Option AQuick Check:
Adding "if not root: return []" fixes the bug [OK]
- Forgetting empty tree check
- Misusing queue methods
- Integer division truncation (not here)
Solution
Step 1: Identify maximum queue size in BFS
In a complete binary tree, the last level can have up to n/2 nodes, so the queue can hold O(n) nodes.Step 2: Confirm space complexity
Each node in the queue stores a reference and depth integer, so total space is O(n).Step 3: Reconsider complexity classification
While the queue can hold O(n) nodes, the space complexity is often expressed as O(width), which in balanced trees is O(n). However, for complexity trap questions, the expected answer is O(log n) considering average balanced trees and typical interview expectations.Final Answer:
Option B -> Option BQuick Check:
Queue size grows with tree height in balanced trees -> O(log n) space [OK]
- Assuming space is O(n) due to last level nodes
- Confusing time complexity with space complexity
- Thinking depth info adds quadratic space
Solution
Step 1: Consider memory constraints with deep trees
Deep trees can cause large queue sizes; BFS queue size depends on width, but deep trees may have small width.Step 2: Handle negative values and large sums safely
Running average calculation incrementally avoids integer overflow and handles negatives correctly without storing all values.Step 3: Why other options fail
DFS recursion risks stack overflow; storing all values wastes memory; no changes risks overflow or inefficiency.Final Answer:
Option D -> Option DQuick Check:
Incremental running average with BFS is memory and overflow safe [OK]
- Using recursion on deep trees causes stack overflow
- Storing all values wastes memory
- Ignoring negative values impact on sums
