Deleting from a binary search tree has three cases, distinguished by how many children the target node has:
| Case | Action |
|---|---|
| 0 children (leaf) | Unlink directly. |
| 1 child | Splice out: connect its parent to its single child. |
| 2 children | You cannot just remove — both subtrees would be orphaned. Replace 's key with a neighbouring key, then delete that node instead. |
The two-children rule
The only key that may take 's place without breaking the BST ordering is an immediate in-order neighbour of . There are exactly two choices:
- the in-order predecessor — the largest key in the left subtree, found by going left once and then right as far as possible (the rightmost node of the left subtree); or
- the in-order successor — the smallest key in the right subtree, found by going right once and then left as far as possible (the leftmost node of the right subtree).
Why these work: the predecessor is the value just below in sorted order, so every key in the left subtree stays it and every key in the right subtree stays it — the BST invariant is preserved. Crucially, both the predecessor and the successor have at most one child (the predecessor has no right child; the successor has no left child), so once you copy the key up, removing the original node falls into the easy 0- or 1-child case.
How to apply it
- Locate the node to delete and confirm it has two children.
- Pick a strategy — say, the in-order successor = leftmost node of 's right subtree.
- Copy 's key into (overwrite 's key; structure of the tree is unchanged so far).
- Recursively delete from the right subtree. Since is leftmost, it has no left child, so this is a 0- or 1-child deletion.
The whole operation walks one root-to-leaf path to find and another to find the predecessor/successor, so it costs time, where is the height ( if balanced, in the worst case).
Worked mini-example
Delete the root from:
50
/ \
30 70
/ \ / \
20 40 60 80
- Predecessor route: largest key in the left subtree = rightmost of = . Replace with , then delete the old (a leaf).
- Successor route: smallest key in the right subtree = leftmost of = . Replace with , then delete the old (a leaf).
Either replacement keeps the tree a valid BST. So the candidates that can replace the deleted root are exactly the predecessor () and the successor () — no other node qualifies.
Exam trick: when a question asks "which element can replace the deleted (two-child) node?", the answer is always one of just two nodes: the rightmost node of the left subtree (predecessor) or the leftmost node of the right subtree (successor). Any other node in the tree is a distractor. Remember: predecessor = left-then-all-right; successor = right-then-all-left.