Practice
Solution
Step 1: Understand the problem constraints
The iterator must return elements in ascending order without storing all nodes upfront to save space.Step 2: Evaluate approaches
Full inorder traversal (B) uses O(n) space upfront, breadth-first (C) does not guarantee sorted order, and divide-and-conquer merging (D) is inefficient per next() call. Controlled inorder traversal with a stack (A) lazily fetches the next smallest element using O(h) space, which is optimal for this problem.Final Answer:
Option D -> Option DQuick Check:
Lazy stack-based inorder traversal matches the problem requirements [OK]
- Thinking BFS returns sorted order
- Assuming full traversal is always best
- Confusing divide-and-conquer with lazy iteration
Solution
Step 1: Identify worst-case scenario
Worst case occurs when all nodes fall within the range or pruning is ineffective, so all n nodes are visited.Step 2: Analyze time complexity
Each node is processed once, so time complexity is O(n). Stack operations are O(1) amortized per node.Final Answer:
Option C -> Option CQuick Check:
Worst case is full traversal, O(n) time [OK]
- Assuming pruning always reduces to O(log n)
- Confusing height h with number of nodes n
def searchBST(root, val):
if root is None:
return None
if root.val == val:
return root
left_search = searchBST(root.left, val)
if left_search is not None:
return left_search
return searchBST(root.right, val)Solution
Step 1: Understand the code logic
This code searches both left and right subtrees regardless of BST property.Step 2: Identify inefficiency bug
Line 5 always searches left subtree even if val > root.val, ignoring BST property and causing O(n) time.Final Answer:
Option A -> Option AQuick Check:
Ignoring BST property causes unnecessary subtree searches [OK]
- Not pruning search using BST property
Solution
Step 1: Identify time complexity
The algorithm visits each node exactly once in inorder traversal, so time is O(n).Step 2: Identify space complexity
Space is O(h) due to recursion stack, where h is the height of the tree; no extra arrays store all nodes.Final Answer:
Option A -> Option AQuick Check:
Linear time and stack space proportional to tree height [OK]
- Confusing recursion stack space with storing all nodes
- Assuming O(n log n) due to sorting
- Ignoring recursion stack space
next() that return the same element multiple times (i.e., allow reuse of elements). Which modification to the Morris traversal based iterator is correct?Solution
Step 1: Understand reuse requirement
Allowing repeated next() calls to return the same element requires storing elements since Morris traversal advances and loses previous state.Step 2: Evaluate modifications
Removing thread restoration (A) corrupts tree, modifying _advance() to not move current (C) breaks traversal logic, and pushing nodes multiple times on stack (B) is inefficient and incorrect. Storing all elements upfront (D) enables repeated next() calls reliably.Final Answer:
Option B -> Option BQuick Check:
Pre-storing elements supports repeated next() calls without traversal [OK]
- Assuming traversal can be paused and repeated
- Ignoring tree corruption from missing restoration
- Trying to simulate reuse with threads
