Practice
Solution
Step 1: Analyze inorder traversal time
Inorder traversal visits each node once -> O(n).Step 2: Analyze balanced BST construction time
Rebuilding uses indices to pick middle nodes without extra sorting -> O(n).Step 3: Consider sorting complexity
Although inorder traversal produces sorted nodes, if sorting is mistakenly applied, complexity becomes O(n log n).Final Answer:
Option A -> Option AQuick Check:
Sorting nodes during traversal leads to O(n log n) [Trap]
- Assuming sorting is not needed, leading to O(n)
- Thinking recursive rebuild is O(n^2) due to repeated subtree calls
- Confusing traversal and construction complexities
next() in the Morris traversal based BST Iterator, assuming the BST has n nodes?Solution
Step 1: Understand Morris traversal edge visits
Each edge in the BST is visited at most twice: once to create a thread and once to remove it.Step 2: Calculate amortized cost per next()
Since total work is O(n) for n nodes, and there are n calls to next(), average cost per call is O(1) amortized.Final Answer:
Option A -> Option AQuick Check:
Amortized O(1) per next() matches Morris traversal properties [OK]
- Assuming worst-case O(n) per call
- Confusing with stack-based O(h)
- Ignoring amortized analysis
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
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
Solution
Step 1: Understand variant allowing duplicates only on right subtree
Duplicates allowed only if they appear in right subtree, so inorder sequence can have equal values only when moving right.Step 2: Modify comparison accordingly
During inorder traversal, allow node.val == prev[0] only if current node is right child of previous node; otherwise, enforce strict inequality.Step 3: Reason why other options fail
Change the comparison to node.val < prev[0] to allow duplicates on the right subtree. allows duplicates anywhere; Change the comparison to node.val <= prev[0] to allow duplicates anywhere. allows duplicates anywhere; Change the comparison to node.val < prev[0] only when traversing left subtree, and node.val <= prev[0] when traversing right subtree. is ambiguous and not implementable in simple inorder traversal without parent info.Final Answer:
Option B -> Option BQuick Check:
Allow duplicates only on right subtree requires conditional equality check [OK]
- Allowing duplicates anywhere
- Ignoring subtree direction in comparison
- Using simple <= everywhere
