Delete from a BST
32 min · Intermediate
Need help? Ask a tutor on WhatsApp (opens in a new tab)Deleting a node from a binary search tree preserves ordering across three child structure cases.
Three deletion cases and leaf nodes
Deleting a node from a binary search tree requires maintaining the ordering invariant. There are three structural cases based on how many children the target node possesses.
// Case 1: a leaf -> return null, so the parent's link becomes nullif (node.left == null && node.right == null) return null;// Case 2: one child -> return that child, so the parent links straight to itif (node.left == null) return node.right;if (node.right == null) return node.left;The simplest case is deleting a leaf node with zero children. Simply disconnect the leaf by setting the parent pointer to null.
The second case is deleting a node with exactly one child. Bypass the target node by linking the parent directly to the single child. The child subtree retains its valid ordering relative to ancestors.
Deleting a node with two children using successors
The inorder successor of a node is the next larger value in the tree. For a node with a right subtree it is the smallest node in that right subtree.
The third case is deleting a node with two children. You cannot delete the node directly without fragmenting the tree into disconnected subtrees. Instead, replace the node value with its inorder successor.
Deleting 10, a node with two children
Step 1 of 6
Example 1 · Standard BST Deletion
public class Main { static class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int val) { this.val = val; } }
static TreeNode findMin(TreeNode node) { while (node.left != null) node = node.left; return node; }
static TreeNode deleteNode(TreeNode root, int key) { if (root == null) return null; 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; if (root.right == null) return root.left;
TreeNode successor = findMin(root.right); root.val = successor.val; root.right = deleteNode(root.right, successor.val); } return root; }
static void inorder(TreeNode root) { if (root == null) return; inorder(root.left); System.out.print(root.val + " "); inorder(root.right); }
public static void main(String[] args) { TreeNode root = new TreeNode(5); root.left = new TreeNode(3); root.right = new TreeNode(6); root.left.left = new TreeNode(2); root.left.right = new TreeNode(4); root.right.right = new TreeNode(7);
root = deleteNode(root, 3); inorder(root); System.out.println(); }}2 4 5 6 7
What this code does
- Locate target node using binary search tree recursive navigation.
- When two children exist, swap value with the right subtree minimum.
- Recursively delete the successor from the right branch.
Example 2 · Inorder Predecessor Deletion Variant
public class Main { static class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int val) { this.val = val; } }
static TreeNode findMax(TreeNode node) { while (node.right != null) node = node.right; return node; }
static TreeNode deleteUsingPredecessor(TreeNode root, int key) { if (root == null) return null; if (key < root.val) { root.left = deleteUsingPredecessor(root.left, key); } else if (key > root.val) { root.right = deleteUsingPredecessor(root.right, key); } else { if (root.left == null) return root.right; if (root.right == null) return root.left;
TreeNode pred = findMax(root.left); root.val = pred.val; root.left = deleteUsingPredecessor(root.left, pred.val); } return root; }
public static void main(String[] args) { TreeNode root = new TreeNode(10); root.left = new TreeNode(5); root.right = new TreeNode(15); root.left.right = new TreeNode(8);
root = deleteUsingPredecessor(root, 10); System.out.println("New root: " + root.val); System.out.println("Left child: " + root.left.val); }}New root: 8 Left child: 5
What this code does
- The predecessor is the maximum value in the left subtree.
- Copying predecessor value maintains valid BST ordering.
- The predecessor is subsequently deleted from the left branch.
Example 3 · Delete Minimum Node Helper
public class Main { static class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int val) { this.val = val; } }
static TreeNode deleteMin(TreeNode node) { if (node.left == null) return node.right; node.left = deleteMin(node.left); return node; }
public static void main(String[] args) { TreeNode root = new TreeNode(20); root.left = new TreeNode(10); root.right = new TreeNode(30); root.left.left = new TreeNode(5);
root = deleteMin(root); System.out.println("New minimum in left branch: " + root.left.val); }}New minimum in left branch: 10
What this code does
- Recurse left until finding the node whose left child is null.
- Bypass the minimum node by returning its right child pointer.
- This helper streamlines successor removal in two-child deletion.
The inorder successor is the smallest value in the right subtree. Find it by moving one step right and walking left until reaching null. Copy the successor value into the target node. Then delete the original successor node from the right subtree. Because the successor has at most one child, deleting it reduces to case one or two.
Order of execution
Delete a node with two children
Tap the steps in the order they happen.
Edge cases and deletion complexity
Deleting the tree root requires returning the new root pointer to the caller. If the root was a leaf, the tree becomes empty.
root = deleteNode(root, 8); // always store the result: the root itself may be replacedif (root == null) { System.out.println("Tree is now empty");}If the root had two children, its replacement becomes the new root.
Finding the successor and relinking takes time proportional to height, running in O(h) time. On a balanced tree, deletion takes O(log n) time.
Next step
Check your understanding
10 quick questions