Practice
class BSTIterator:
def __init__(self, root):
self.current = root
self.next_val = None
self._advance()
def _advance(self):
while self.current:
if not self.current.left:
self.next_val = self.current.val
self.current = self.current.right
return
else:
pre = self.current.left
while pre.right and pre.right != self.current:
pre = pre.right
if not pre.right:
pre.right = self.current
self.current = self.current.left
else:
pre.right = None
self.next_val = self.current.val
self.current = self.current.right
Given the BST with nodes 2 (root), 1 (left child), and 3 (right child), what is the value of next_val after the first call to _advance() during initialization?Solution
Step 1: Trace _advance() starting at root=2
Since root has a left child (1), find predecessor of 2 in left subtree: node 1 has no right child, so set 1.right = 2 and move current to 1.Step 2: At current=1, no left child, so set next_val=1 and move current to 1.right which points back to 2 (thread).
Return from _advance() with next_val=1.Final Answer:
Option C -> Option CQuick Check:
First inorder element is 1, matches next_val after first advance [OK]
- Assuming next_val is root value initially
- Forgetting thread creation
- Confusing next_val with current node
s during the first iteration of the while loop?Solution
Step 1: Perform inorder traversal
Inorder traversal of BST yields sorted array: [2, 3, 4, 5, 6, 7]Step 2: Calculate s in first iteration
left=0 (value=2), right=5 (value=7), s = 2 + 7 = 9Final Answer:
Option A -> Option AQuick Check:
Sum matches target k=9 on first iteration [OK]
- Off-by-one error in indexing left or right
- Confusing node values with indices
- Miscomputing sum s
Solution
Step 1: Identify number of nodes visited
Each node is visited exactly once during traversal.Step 2: Analyze stack operations and overall traversal
While each node is visited once, the explicit stack can hold up to h nodes, and each push/pop operation can be considered O(1). However, in the worst case (unbalanced tree), the height h can be up to n, making the complexity O(n * h).Final Answer:
Option B -> Option BQuick Check:
Worst-case complexity depends on tree height [OK]
- Assuming stack operations multiply complexity by height unnecessarily
- Confusing recursion stack space with time complexity
- Thinking balanced BST implies O(n log n) time
Solution
Step 1: Understand base case condition
The base case should be when left > right, not left >= right, to allow single-element subarrays.Step 2: Consequence of incorrect base case
Using left >= right causes the recursion to skip processing subarrays of size 1, leading to infinite recursion or missing nodes.Final Answer:
Option D -> Option DQuick Check:
Base case must allow left == right to process single elements [OK]
- Incorrect base case causing infinite recursion
- Misplaced mid calculation
Solution
Step 1: Identify traversal path length
The algorithm moves from root down one path until the split point, visiting at most h nodes.Step 2: Understand h vs n
Height h can be up to n in worst case (skewed tree), but the question asks for optimal algorithm time complexity, which assumes balanced BST where h = log n.Final Answer:
Option A -> Option AQuick Check:
Traversal limited to one root-to-node path in balanced BST [OK]
- Confusing worst-case O(n) with average-case O(log n)
- Assuming constant time due to simple comparisons
- Ignoring that recursion stack space is not used here
