Practice
Solution
Step 1: Trace reverse inorder traversal order
Nodes visited in order: 3, 2, 1.Step 2: Accumulate sums and update nodes
acc_sum=0 initially. - Visit 3: acc_sum=3, node.val=3 - Visit 2: acc_sum=3+2=5, node.val=5 - Visit 1: acc_sum=5+1=6, node.val=6 Root node was 2, updated to 5.Final Answer:
Option A -> Option AQuick Check:
Root node value after update is 5 [OK]
- Confusing traversal order and updating nodes too early
- Off-by-one errors in accumulating sums
- Misidentifying which node is root after updates
Solution
Step 1: Trace first iteration with current=6
p=2 and q=8; 2 < 6 and 8 > 6, so current is split point; return 6.Step 2: Confirm no further traversal
Since p and q lie on different sides of 6, 6 is the LCA.Final Answer:
Option D -> Option DQuick Check:
Split point found at root 6 immediately [OK]
- Returning smaller node instead of split point
- Confusing left/right subtree traversal
- Off-by-one error in loop conditions
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def searchBST(root, val):
current = root
while current:
if current.val == val:
return current
elif val < current.val:
current = current.left
else:
current = current.right
return None
root = TreeNode(4, TreeNode(2, TreeNode(1), TreeNode(3)), TreeNode(7))
result = searchBST(root, 3)
print(result.val if result else 'null')Solution
Step 1: Trace the search path
Start at root (4). Since 3 < 4, move to left child (2). Since 3 > 2, move to right child (3).Step 2: Check node value
Current node value is 3, which matches the search value, so return this node.Final Answer:
Option D -> Option DQuick Check:
Search follows BST property correctly and finds node with value 3 [OK]
- Returning wrong node value due to off-by-one traversal
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 how duplicates affect sum accumulation
Duplicates must be included in the sum for nodes with equal values to maintain correctness.Step 2: Modify accumulation logic
Accumulate sum including all nodes with values >= current node before updating node.val during reverse inorder traversal.Final Answer:
Option D -> Option DQuick Check:
Including equal values in sum ensures correct Greater Tree with duplicates [OK]
- Changing traversal order breaks sum logic
- Skipping duplicates causes incorrect sums
- Using extra data structures unnecessarily increases complexity
