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: 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
def recoverTree(root):
first = second = prev = None
current = root
while current:
if not current.left:
if prev and prev.val > current.val:
if not first:
first = prev
second = current
else:
second = current
prev = current
current = current.right
else:
pred = current.left
while pred.right and pred.right != current:
pred = pred.right
if not pred.right:
pred.right = current
current = current.left
else:
pred.right = None
if prev and prev.val > current.val:
if not first:
first = prev
second = current
else:
second = current
prev = current
current = current.right
first.val, second.val = second.val, first.val
Solution
Step 1: Analyze detection of swapped nodes
The code sets first and second whenever a violation is found, but overwrites them without preserving the first violation's first node.Step 2: Identify the bug in swapping
The swap at the end assumes both first and second are found, but if only one violation is detected (adjacent nodes swapped), second may be incorrect or null, causing wrong swap.Final Answer:
Option A -> Option AQuick Check:
Swapping without confirming both nodes leads to incorrect recovery in adjacent swaps [OK]
- Overwriting first and second on multiple violations
- Swapping values prematurely after first violation
- Not restoring tree structure after Morris traversal
Solution
Step 1: Understand duplicates in BST with relaxed property
Inorder traversal still produces nodes in non-decreasing order preserving duplicates order.Step 2: Rebuild balanced BST using middle node selection
Choosing middle node as root maintains balance and respects duplicates order.Step 3: Avoid sorting or preorder traversal
Sorting is unnecessary and preorder breaks sorted order; left middle bias skews balance.Final Answer:
Option C -> Option CQuick Check:
Inorder traversal + middle node rebuild works with duplicates [OK]
- Using preorder traversal losing sorted order
- Sorting nodes again causing unnecessary overhead
- Biasing middle index causing unbalanced tree
