Skip to content
Problem 450 · LeetCode 450(opens in a new tab)

Delete Node in a BST

MediumBinary search trees

You get the root of a binary search tree (BST) and a number key. In a BST, every value in a node's left subtree is smaller than the node, and every value in its right subtree is larger.

Remove the node whose value is key, if there is one, so that what remains is still a valid BST. Return the root of the result, which may be a different node than before. When several shapes are valid, any of them is accepted.

Example 1

Input:  root = [8, 4, 12, 2, 6, null, 14], key = 4
Output: [8, 6, 12, 2, null, null, 14]
Why:    6, the next value after 4, takes its place
2 more examples and the constraints

Example 2

Input:  root = [8, 4, 12, 2, 6, null, 14], key = 5
Output: [8, 4, 12, 2, 6, null, 14]
Why:    5 is not in the tree, so nothing changes

Example 3

Input:  root = [3, null, 9], key = 3
Output: [9]
Why:    the root has one child, which becomes the new root

Constraints

  • 0 ≤ number of nodes ≤ 10,000
  • −100,000 ≤ Node.val, key ≤ 100,000
  • All values are different, and the input is a valid BST
  • Follow-up: work in O(height of the tree)

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Collect and rebuildO(n)
  2. Walk down, fix in placeO(h)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for 10,000 nodes, balancedVerdict
Collect and rebuildO(n)
Correct, but touches every node
Walk down, fix in placeO(h)
Best; answers the follow-up

n = number of nodes, h = height of the tree (about log n when balanced, up to n when not).

Edge cases to test

  • Empty tree. Return null; there is nothing to delete.
  • Key not present. The walk falls off the tree and every call returns its node unchanged.
  • Deleting the root. [3, null, 9] with key 3 returns 9 as the new root, so the caller must use the return value.
  • Successor with a right child. In [20, 10, 30, null, null, 25, 40, null, 27], deleting 20 moves 25 up and 27 takes 25's old place.

Common mistakes

  • Ignoring the return value and losing the change when the root itself is deleted.
  • Taking the leftmost node of the whole tree instead of the right subtree as the successor.
  • Copying the successor's value but forgetting to delete the original successor node.

How to talk through it

  1. 1Clarify. Are values unique? Can the key be missing? Is any valid BST shape accepted?
  2. 2Give the simple version. Collect the sorted values without the key and rebuild; O(n).
  3. 3Use the order. Walk down like a search, so only one path is touched: O(h).
  4. 4Name the three cases. No child, one child, two children; the last uses the in-order successor.
  5. 5Test out loud. Delete a leaf, a root with one child, a missing key and a node whose successor has a right child.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?