Skip to content
StackDecode
TreesAnimated

BST Deletion

Remove a leaf, bypass a single child, or replace with the in-order successor.

Time

O(h)

Extra space

O(1)

Use it for

Maintaining BSTs.

How it works

  1. Find the node to delete.
  2. Leaf: just remove it. One child: replace the node by that child.
  3. Two children: find the in-order successor (the smallest key in the right subtree), copy its key into the node, then delete the successor from the right subtree.
  4. The tree stays a valid BST. O(height).

Watch it run

Loading animation…