✓ The recursive search guided by BST properties efficiently locates the node to delete.
Without visualization, it's hard to see how recursion narrows down the search path.
✓ Replacing a node with its inorder successor preserves BST order after deletion.
This insight is subtle in code but clear when watching the successor being found and replacing the node.
✓ Deleting the successor node recursively handles the duplicate value cleanly.
The recursive deletion of the successor node is a key step that can be overlooked without step-by-step tracing.
Practice
(1/5)
1. You are given a sorted array of unique integers and need to construct a binary search tree with minimal height. Which approach guarantees the resulting tree is height-balanced and constructed in O(n) time?
easy
A. Use a divide-and-conquer recursion that picks the middle element as root and recursively builds left and right subtrees.
B. Insert elements one by one into an empty BST using standard BST insertion.
C. Use a greedy approach that always inserts the smallest remaining element as the left child.
D. Build a balanced BST by repeatedly merging two smaller balanced BSTs.
Solution
Step 1: Understand the problem constraints
The input is a sorted array, and the goal is to build a height-balanced BST efficiently.
Step 2: Evaluate approaches
Inserting elements one by one leads to skewed trees and O(n^2) time. Greedy insertion of smallest elements does not guarantee balance. Merging BSTs is not straightforward and inefficient. The divide-and-conquer approach picks the middle element as root, ensuring balanced subtrees and O(n) time.
Final Answer:
Option A -> Option A
Quick Check:
Middle element chosen recursively -> balanced BST in O(n) time [OK]
Hint: Middle element recursion ensures balanced BST [OK]
Common Mistakes:
Thinking inserting one by one is efficient
Assuming greedy insertion balances tree
2. Given the following code snippet for converting a sorted array to a height-balanced BST, what is the value of the root node's value after the first call to helper with input nums = [1, 3, 5]?
easy
A. 3
B. 1
C. 5
D. None
Solution
Step 1: Calculate mid index for initial call
For nums=[1,3,5], left=0, right=2, mid=(0+2)//2=1.
Step 2: Identify root value
nums[mid] = nums[1] = 3, so root node value is 3.
Final Answer:
Option A -> Option A
Quick Check:
Mid index calculation picks 3 as root [OK]
Hint: Mid index picks middle element as root [OK]
Common Mistakes:
Choosing first or last element as root
Off-by-one mid calculation
3. Given the following code for finding the kth smallest element in a BST, what is the returned value when called with k=2 on the provided tree?
from typing import Optional
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
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
# Tree:
# 3
# / \
# 1 4
# \
# 2
root = TreeNode(3)
root.left = TreeNode(1)
root.right = TreeNode(4)
root.left.right = TreeNode(2)
print(kthSmallest(root, 2))
easy
A. 3
B. 1
C. 2
D. 4
Solution
Step 1: Trace the inorder traversal steps
Stack initially empty, current=root(3). Push 3, move left to 1. Push 1, move left to None. Pop 1 (count=1), move right to 2.
Step 2: Continue traversal to find kth=2
Push 2, move left None. Pop 2 (count=2), return 2 as kth smallest.
Final Answer:
Option C -> Option C
Quick Check:
Second smallest in BST is 2 [OK]
Hint: Inorder traversal visits nodes in ascending order [OK]
Common Mistakes:
Off-by-one counting of k
Returning before incrementing count
4. What is the worst-case time complexity of the optimized iterative DFS with BST pruning approach for computing the range sum of a BST with n nodes and height h?
medium
A. O(h) because only nodes along the height are visited
B. O(log n) because pruning skips most nodes
C. O(n) because in worst case pruning does not skip any nodes
D. O(n log n) because each node may be visited multiple times due to stack operations
Solution
Step 1: Identify worst-case scenario
Worst case occurs when all nodes fall within the range or pruning is ineffective, so all n nodes are visited.
Step 2: Analyze time complexity
Each node is processed once, so time complexity is O(n). Stack operations are O(1) amortized per node.
Final Answer:
Option C -> Option C
Quick Check:
Worst case is full traversal, O(n) time [OK]
Hint: Worst case equals full traversal of all nodes [OK]
Common Mistakes:
Assuming pruning always reduces to O(log n)
Confusing height h with number of nodes n
5. Suppose the BST can contain duplicate values and the range is inclusive [low, high]. How should the trimming algorithm be modified to correctly handle duplicates without breaking BST properties?
hard
A. Change all comparisons to strict inequalities (e.g., < low to <= low) to exclude duplicates outside range.
B. Rebuild the BST from scratch after filtering duplicates to ensure correct ordering.
C. Adjust pruning conditions to include nodes equal to low or high by using <= and >= comparisons appropriately.
D. Ignore duplicates during pruning since BST properties guarantee uniqueness.
Solution
Step 1: Understand duplicates in BST
Duplicates can be on left or right subtree depending on implementation; inclusive range means nodes equal to low or high must be kept.
Step 2: Modify pruning conditions
Use <= and >= in comparisons to keep nodes equal to boundaries, ensuring duplicates within range remain.
Step 3: Avoid rebuilding
Rebuilding wastes time and space; pruning with adjusted conditions preserves BST and duplicates correctly.
Final Answer:
Option C -> Option C
Quick Check:
Inclusive pruning keeps duplicates at boundaries [OK]
Hint: Use inclusive comparisons to keep duplicates within range [OK]
Common Mistakes:
Using strict inequalities excludes boundary duplicates
Rebuilding unnecessarily wastes resources
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.