What are the three main cases to consider when deleting a node in a Binary Search Tree (BST)?
1. Node to delete is a leaf (no children). 2. Node to delete has one child. 3. Node to delete has two children.
Click to reveal answer
intermediate
In BST deletion, how do you handle deleting a node with two children?
Replace the node's value with its inorder successor (smallest value in right subtree) or inorder predecessor (largest value in left subtree), then delete that successor or predecessor node.
Click to reveal answer
beginner
What is the inorder successor in a BST?
The node with the smallest value in the right subtree of a given node.
Click to reveal answer
intermediate
Why do we replace a node with its inorder successor or predecessor when deleting a node with two children in a BST?
Because it maintains the BST property by ensuring all left subtree nodes are smaller and right subtree nodes are larger than the replaced node.
Click to reveal answer
beginner
What happens if you delete a leaf node in a BST?
You simply remove the leaf node by setting its parent's pointer to null.
Click to reveal answer
Which of the following is NOT a case when deleting a node in a BST?
ANode has two children
BNode has one child
CNode has three children
DNode has no children
✗ Incorrect
A BST node can have at most two children, so 'three children' is not a valid case.
When deleting a node with two children, what is the first step?
AReplace the node with its right child
BRemove the node immediately
CReplace the node with its left child
DReplace the node's value with its inorder successor or predecessor
✗ Incorrect
We replace the node's value with its inorder successor or predecessor to maintain BST properties.
What is the inorder successor of a node in a BST?
ASmallest node in right subtree
BLargest node in left subtree
CParent node
DAny leaf node
✗ Incorrect
The inorder successor is the smallest node in the right subtree.
How do you delete a leaf node in a BST?
ASet parent's pointer to null
BReplace it with inorder successor
CReplace it with inorder predecessor
DReplace it with its child
✗ Incorrect
Since leaf nodes have no children, just set the parent's pointer to null.
Why is the inorder successor used in deletion instead of any random node?
AIt is always a leaf node
BIt maintains BST order after replacement
CIt is the root node
DIt has two children
✗ Incorrect
Using the inorder successor keeps the BST property intact after deletion.
Explain the steps to delete a node with two children in a BST.
Think about how to keep the tree ordered after deletion.
You got /4 concepts.
Describe how deleting a leaf node differs from deleting a node with one child in a BST.
Consider what happens to the parent's pointer in each case.
You got /3 concepts.
Practice
(1/5)
1. What happens when you delete a leaf node (a node with no children) from a Binary Search Tree (BST)?
easy
A. The node is simply removed and its parent's pointer is set to null.
B. The entire tree is restructured to maintain balance.
C. The node is replaced by its in-order predecessor.
D. The node is replaced by its in-order successor.
Solution
Step 1: Understand leaf node deletion
A leaf node has no children, so removing it does not affect other nodes.
Step 2: Effect on parent pointer
The parent's pointer to this node is set to null, effectively removing the node.
Final Answer:
The node is simply removed and its parent's pointer is set to null. -> Option A
Quick Check:
Leaf node deletion = remove node and set parent's pointer null [OK]
Hint: Leaf node deletion just removes the node, no restructuring needed [OK]
Common Mistakes:
Thinking the tree needs full restructuring
Replacing leaf node with predecessor or successor
Confusing leaf node deletion with internal node deletion
2. Which of the following is the correct Java method signature for deleting a node with a given key from a BST?
easy
A. public boolean deleteNode(int key)
B. public void deleteNode(int key)
C. public int deleteNode(Node root, int key)
D. public Node deleteNode(Node root, int key)
Solution
Step 1: Understand BST delete method signature
The method usually takes the root node and the key to delete, returning the updated root.
Step 2: Match method signature
public Node deleteNode(Node root, int key) matches: it takes Node root and int key, returns Node (updated root).
Final Answer:
public Node deleteNode(Node root, int key) -> Option D
Quick Check:
Delete method returns updated root node [OK]
Hint: Delete method returns updated root node after deletion [OK]
Common Mistakes:
Using void return type instead of Node
Not passing root node as parameter
Returning boolean or int instead of Node
3. Given the BST below, what is the in-order traversal after deleting the node with value 10?
15
/ \
10 20
/ \ \
8 12 25
medium
A. [8, 12, 15, 20, 25, 10]
B. [8, 12, 15, 20, 25]
C. [8, 12, 15, 20]
D. [8, 10, 12, 15, 20, 25]
Solution
Step 1: Delete node 10 from BST
Node 10 has two children (8 and 12). Replace it with its in-order successor (12) and delete 12.
Hint: Replace deleted node with in-order successor for two children case [OK]
Common Mistakes:
Not replacing node 10 correctly
Including deleted node 10 in traversal
Confusing in-order successor with predecessor
4. Consider this Java method snippet for deleting a node from a BST:
public Node deleteNode(Node root, int key) {
if (root == null) return root;
if (key < root.val) root.left = deleteNode(root.left, key);
else if (key > root.val) root.right = deleteNode(root.right, key);
else {
if (root.left == null) return root.right;
else if (root.right == null) return root.left;
Node temp = minValueNode(root.right);
root.val = temp.val;
root.right = deleteNode(root.right, key);
}
return root;
}
private Node minValueNode(Node node) {
Node current = node;
while (current.left != null) current = current.left;
return current;
}
What is the bug in this code?
medium
A. The recursive call to deleteNode uses key instead of temp.val.
B. The method does not handle the case when root is null.
C. The method minValueNode should return the maximum value node instead.
D. The method does not update root.left or root.right correctly.
Solution
Step 1: Analyze recursive delete call after replacement
After replacing root.val with temp.val (in-order successor), the successor must be deleted from the right subtree using temp.val.
Step 2: Identify the bug
The recursive call uses key (original value) instead of temp.val. Since key < values in right subtree, it won't correctly delete the successor.
Step 3: Check other options
A is wrong (minValueNode finds successor correctly). B is wrong (null root handled). D is wrong (updates via recursion).
Final Answer:
The recursive call to deleteNode uses key instead of temp.val. -> Option A
Quick Check:
Recursive call must use temp.val [OK]
Hint: Check recursive calls carefully for correct key usage [OK]
Common Mistakes:
Thinking minValueNode should find max value
Confusing key and temp.val in recursive call
Missing base case for null root
5. You have a BST with nodes: 30, 20, 40, 10, 25, 35, 50. You delete node 20. Which of the following is the correct BST in-order traversal after deletion?
hard
A. [10, 25, 30, 40, 50]
B. [10, 20, 25, 30, 35, 40, 50]
C. [10, 25, 30, 35, 40, 50]
D. [25, 30, 35, 40, 50]
Solution
Step 1: Identify node 20's children
Node 20 has two children: 10 (left) and 25 (right).
Step 2: Delete node 20 by replacing with in-order successor
In-order successor is 25. Replace 20 with 25, then delete node 25.