Practice
second.val after the traversal completes?Solution
Step 1: Trace inorder traversal with Morris method
The inorder traversal visits nodes in order: 1, 3, 2, 4. The violations are detected when prev.val > current.val: first at (3,2).Step 2: Identify swapped nodes
First violation sets first=3, second=2. No second violation occurs, so second remains 2.Final Answer:
Option B -> Option BQuick Check:
Second node detected is the node with value 2 [OK]
- Confusing first and second nodes
- Missing second violation for adjacent swaps
- Off-by-one in traversal order
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
def insertIntoBST(root, val):
if root is None:
return TreeNode(val)
if val < root.val:
insertIntoBST(root.left, val)
else:
insertIntoBST(root.right, val)
return root
Solution
Step 1: Analyze recursive calls
Recursive calls to insertIntoBST on root.left or root.right do not update the parent's child pointer.Step 2: Identify missing assignment
Lines 5 and 7 call insertIntoBST but do not assign the returned subtree back to root.left or root.right, so insertion is lost.Final Answer:
Option D -> Option DQuick Check:
Missing assignment causes silent insertion failure [OK]
- Not updating parent's child pointer
- Confusing base case with recursive step
- Incorrect comparison operator
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 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
