Skip to main content
Back to problems
Leetcode
Medium
Trees
Binary Search
Recursion
Google
Delete Node in a BST

Delete a node with a given key from a binary search tree while preserving the BST property.

Acceptance 0%
Problem Statement

Problem

You are given the root of a binary search tree (BST) and an integer key.

Remove the node whose value equals key from the tree, and return the root of the modified BST.

After deletion, the tree must still satisfy the BST ordering property:

  • values in the left subtree are smaller than the node value
  • values in the right subtree are larger than the node value

If the key does not exist in the tree, return the tree unchanged.

When the node to delete has two children, replace it with a valid BST successor or predecessor strategy so the tree remains valid.

Input Format

  • root: the root node of a BST
  • key: integer value to delete

Output Format

Return the root node of the BST after removing key.

Constraints

  • The tree is a valid BST before deletion.
  • Node values are typically distinct.
  • If key is not present, the tree should remain unchanged.
  • Preserve BST ordering after deletion.
Examples
Sample cases returned by the problem API.

Example 1

Input

root = [5,3,6,2,4,null,7], key = 3

Output

[5,4,6,2,null,null,7]

Explanation

Node 3 has two children. Replace it with its inorder successor 4, then delete the original 4 node.

Example 2

Input

root = [5,3,6,2,4,null,7], key = 0

Output

[5,3,6,2,4,null,7]

Explanation

The key is not present, so the BST stays the same.

Show 1 more example

Example 3

Input

root = [], key = 1

Output

[]

Explanation

An empty tree remains empty after deletion.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.