Skip to main content
LeetCode 450, Medium. Topics: Tree, Binary Search Tree, Binary Tree. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 15 parametrized pytest cases, and a playground notebook:

Problem

Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST. Basically, the deletion can be divided into two stages:
  1. Search for a node to remove.
  2. If the node is found, delete the node.
Note: When a node with two children is deleted, replacing it with either its inorder successor or predecessor is accepted.

Examples

Example 1

Constraints

  • The number of nodes in the tree is in the range [0, 10^4].
  • -10^5 <= Node.val <= 10^5
  • Each node has a unique value.
  • root is a valid binary search tree.
  • -10^5 <= key <= 10^5
Follow up: Could you solve it with time complexity O(height of tree)?

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode 250, NeetCode All.
Last modified on August 25, 2026