Practice
Solution
Step 1: Understand BST deletion cases
Deletion must handle three cases: leaf node, node with one child, and node with two children. The two children case requires replacing the node with its in-order successor or predecessor to maintain BST properties.Step 2: Identify approach that handles all cases correctly
The recursive approach that finds the node, replaces it with its in-order successor if it has two children, and recursively deletes the successor ensures correctness and efficiency.Final Answer:
Option C -> Option CQuick Check:
Only Use a recursive approach that finds the node, then if it has two children, replace it with its in-order successor and recursively delete the successor. correctly handles all deletion cases preserving BST [OK]
- Greedy deletion ignoring right subtree breaks BST
- Rebuilding tree is inefficient
- Dynamic programming irrelevant here
Solution
Step 1: Understand the problem constraints
We want to find the kth smallest element in a BST efficiently, avoiding unnecessary traversal.Step 2: Evaluate approaches
Full inorder traversal (A) is correct but not optimal since it traverses the entire tree. Greedy left moves (C) fail because the kth element may not be in the leftmost path. DP (D) is complex and not standard for this problem. Iterative inorder with early stopping (B) visits only needed nodes, achieving O(H + k) time and O(H) space.Final Answer:
Option B -> Option BQuick Check:
Iterative inorder with early stopping avoids full traversal [OK]
- Assuming full traversal is necessary
- Confusing greedy left moves with inorder traversal
def deleteNode(root, key):
if not root:
return null
if key < root.val:
root.left = deleteNode(root.left, key)
elif key > root.val:
root.right = deleteNode(root.right, key)
else:
if not root.left:
return root.right
elif not root.right:
return root.left
else:
successor = findSuccessor(root)
root.val = successor.val
root.left = deleteNode(root.right, successor.val) # Bug here
return rootSolution
Step 1: Identify two-children deletion logic
When deleting a node with two children, we replace its value with the successor's value and then delete the successor from the right subtree.Step 2: Spot incorrect subtree assignment
The line assigns root.left = deleteNode(root.right, successor.val), which incorrectly deletes from the right subtree but assigns result to left child, breaking BST structure.Final Answer:
Option A -> Option AQuick Check:
Correct line should assign root.right = deleteNode(root.right, successor.val) [OK]
- Mixing left and right subtree assignments
- Forgetting to update parent's child pointer
- Replacing value but not deleting successor
def lowestCommonAncestor(root, p, q):
current = root
while current:
if p.val < current.val and q.val < current.val:
current = current.left
elif p.val > current.val and q.val > current.val:
current = current.right
else:
return current
Solution
Step 1: Analyze comparison operators
Using '<=' and '>=' causes the loop to move past the node when p or q equals current, missing the case where current is ancestor of itself.Step 2: Correct operator usage
Changing to '<' and '>' ensures the loop stops at the correct ancestor node, including when one node is ancestor of the other.Final Answer:
Option A -> Option AQuick Check:
Inclusive comparisons skip valid ancestor nodes [OK]
- Assuming ancestor must be strictly above nodes
- Ignoring null root edge cases
- Returning too early without validation
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
