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 BST property usage
In a BST, left subtree nodes are smaller and right subtree nodes are larger than the current node. This allows pruning the search space efficiently.Step 2: Identify optimal traversal
Starting from root, if both target nodes are smaller, go left; if both are larger, go right; else current node is the split point and thus the LCA.Final Answer:
Option C -> Option CQuick Check:
Iterative BST traversal uses O(h) time and O(1) space [OK]
- Using path comparison wastes time and space
- Greedy approach ignores split point logic
- Dynamic programming is unnecessary overhead
Solution
Step 1: Identify number of nodes visited
Each node is visited exactly once during traversal.Step 2: Analyze stack operations and overall traversal
While each node is visited once, the explicit stack can hold up to h nodes, and each push/pop operation can be considered O(1). However, in the worst case (unbalanced tree), the height h can be up to n, making the complexity O(n * h).Final Answer:
Option B -> Option BQuick Check:
Worst-case complexity depends on tree height [OK]
- Assuming stack operations multiply complexity by height unnecessarily
- Confusing recursion stack space with time complexity
- Thinking balanced BST implies O(n log n) time
def kthSmallest(root: Optional[TreeNode], k: int) -> int:
stack = []
current = root
count = 0
while current or stack:
while current:
stack.append(current)
current = current.left
current = stack.pop()
count += 1
if count == k:
return current.val
current = current.right
# Bug introduced: k is used as zero-based index (count == k-1) instead of one-based
Solution
Step 1: Identify the off-by-one bug
The condition uses 'count == k - 1' which treats k as zero-based, but k is one-based in problem definition.Step 2: Consequence of bug
This causes the function to return the (k-1)th smallest element or miss the kth element, leading to incorrect results or runtime errors if k=1.Final Answer:
Option A -> Option AQuick Check:
Correct condition must be 'count == k' to match one-based indexing [OK]
- Using zero-based index for k
- Misplacing count increment
- Returning too early
Solution
Step 1: Check root adjustment logic
If root.val < low, root should move to root.right, not root.left, to find valid nodes.Step 2: Verify other lines
Line 5 correctly moves root to root.left if root.val > high. Lines 10 and 16 correctly adjust subtree pointers.Final Answer:
Option A -> Option AQuick Check:
Incorrect pointer move causes invalid pruning or infinite loop [OK]
- Confusing left and right subtree moves
- Incorrect pointer updates causing invalid BST
