Delete Node in a BST
Pattern: Search, insert and delete
Find the node. With no child, remove it; with one child, let the child take its place. With two children, copy in the smallest value of its right subtree (its inorder successor) and delete that value from the right subtree instead.
The problem
Delete the node holding key (if it exists) and return the root. Several trees are correct, so the tests print the result's inorder and check that it is a valid BST. Trees in the examples are written in level order, with null for a missing child, as on LeetCode.
Think first
- What are the three cases?
- With two children, which value can take the node's place?
- How do you remove that value?
Hints
Brute force, and why it fails
Rebuild the tree from the remaining values: O(n).
The key insight
Copying in the inorder successor keeps the BST rule at the node, and deleting the successor is always one of the easy cases.
Dry run
| at | 3 |
|---|
root = [5, 3, 6, 2, 4, null, 7], key = 3. 3 < 5: go left to 3.
The plan
delete(n, key): if null: null
if key < n.val: n.left = delete(n.left, key)
elif key > n.val: n.right = delete(n.right, key)
else: if no left: return n.right; if no right: return n.left
s = min of n.right; n.val = s; n.right = delete(n.right, s)
return n
Complexity
- Time
O(h)- Space
O(h)
At most two paths down.
Edge cases
- Deleting the root.
- A key that is not there: unchanged.
- Deleting the only node.
I've built my logic, show the code
Java
class Solution {
public 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; // zero or one child: the child takes its place
if (root.right == null) return root.left;
TreeNode next = root.right; // two children: copy in the next larger value
while (next.left != null) next = next.left;
root.val = next.val;
root.right = deleteNode(root.right, next.val); // and delete that value from the right side
}
return root;
}
}C++
class Solution {
public:
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; // zero or one child: the child takes its place
if (root->right == NULL) return root->left;
TreeNode* next = root->right; // two children: copy in the next larger value
while (next->left != NULL) next = next->left;
root->val = next->val;
root->right = deleteNode(root->right, next->val); // and delete that value from the right side
}
return root;
}
};Python
class Solution:
def deleteNode(self, root, key: int):
if root is None:
return None
if key < root.val:
root.left = self.deleteNode(root.left, key)
elif key > root.val:
root.right = self.deleteNode(root.right, key)
else:
if root.left is None:
return root.right # zero or one child: the child takes its place
if root.right is None:
return root.left
nxt = root.right # two children: copy in the next larger value
while nxt.left:
nxt = nxt.left
root.val = nxt.val
root.right = self.deleteNode(root.right, nxt.val) # and delete that value from the right side
return rootCommon mistakes
- Forgetting the two-children case.
- Losing the subtree when a node has one child.