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: Understand BST property
The BST property requires all left subtree nodes to be less than the current node and all right subtree nodes to be greater, strictly increasing in inorder traversal.Step 2: Recognize inorder traversal pattern
Inorder traversal of a valid BST produces a strictly increasing sequence of values, so verifying this sequence guarantees correctness efficiently.Final Answer:
Option D -> Option DQuick Check:
Inorder traversal yields sorted values for BST [OK]
- Only comparing node with immediate children
- Using greedy checks without global ordering
- Confusing subtree min/max with inorder sequence
Solution
Step 1: Understand traversal order needed
Reverse inorder traversal requires visiting right subtree first to accumulate greater values.Step 2: Identify incorrect subtree traversal
Line 8 incorrectly moves to left child first, reversing traversal order and causing wrong sums.Final Answer:
Option A -> Option AQuick Check:
Changing node = node.left to node = node.right fixes traversal order [OK]
- Swapping left and right subtree traversal order
- Resetting acc_sum inside loops
- Updating node values before traversing right subtree
Solution
Step 1: Understand duplicate handling
Duplicates should be consistently placed in the right subtree to maintain BST property.Step 2: Modify insertion logic
Keep mid calculation unchanged for balance; during node creation, ensure duplicates go to right subtree by design.Step 3: Avoid brute force
Brute force insertion leads to unbalanced trees and higher complexity.Final Answer:
Option C -> Option CQuick Check:
Maintain balance and BST property by consistent duplicate placement right [OK]
- Changing mid breaks balance
- Inserting duplicates left breaks BST property
Solution
Step 1: Understand the problem extension
Multiple pairs of nodes swapped means multiple violations, possibly complex to detect and fix in one pass.Step 2: Evaluate approaches
Morris traversal detects only two nodes swapped optimally; multiple swaps require collecting all values, sorting, and rewriting nodes, which brute force does efficiently.Final Answer:
Option C -> Option CQuick Check:
Brute force approach handles multiple swaps correctly by sorting all values [OK]
- Assuming Morris traversal can fix multiple swaps in one pass
- Trying to swap nodes immediately without full detection
- Running multiple passes of Morris traversal inefficiently
