Practice
Solution
Step 1: Understand the problem constraints
The problem requires recovering a BST where two nodes are swapped, without extra space and without changing the tree structure.Step 2: Identify the approach that meets constraints
Morris inorder traversal allows inorder traversal without recursion or stack, detecting swapped nodes on the fly and swapping their values, achieving O(1) space.Final Answer:
Option D -> Option DQuick Check:
Morris traversal is the only approach with O(1) space and no structural changes [OK]
- Assuming sorting values is optimal despite extra space
- Believing greedy single pass fixes all cases
- Confusing DP with tree recovery
Solution
Step 1: Trace first tree (2,1,3)
Inorder traversal yields [1,2,3], strictly increasing, so returns true.Step 2: Trace second tree (5,1,4 with children 3 and 6)
Inorder traversal yields [1,5,3,4,6], 3 < 5 violates BST property, so returns false.Final Answer:
Option A -> Option AQuick Check:
First tree valid BST, second invalid due to subtree violation [OK]
- Assuming subtree root comparison is enough
- Confusing output order
- Missing strict inequality check
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
Solution
Step 1: Understand DFS and set usage
The code checks if complement exists in seen before adding current node's value.Step 2: Identify bug in order of operations
Checking complement before adding current node's value misses pairs where both nodes are the same, or misses valid pairs if current node's value is needed for complement checks in children.Final Answer:
Option B -> Option BQuick Check:
Complement check must happen after adding current node's value to seen [OK]
- Checking complement before adding current node's value
- Not handling None nodes properly
- Returning False too early
Solution
Step 1: Understand duplicates in BST inorder traversal
Inorder traversal visits nodes in sorted order including duplicates as separate nodes.Step 2: Check if modification is needed
Since duplicates appear as separate nodes, counting each node visited naturally counts duplicates separately without extra logic.Step 3: Evaluate other options
Skipping duplicates (B) or using a hash set (C) changes semantics. Removing duplicates after full traversal (D) is inefficient and breaks counting duplicates separately.Final Answer:
Option D -> Option DQuick Check:
Inorder traversal inherently counts duplicates separately [OK]
- Trying to skip duplicates
- Using extra space unnecessarily
- Assuming duplicates must be merged
