Problem
You get the root of a binary search tree and an integer key. The input shows the tree as a list in level order. This order lists the nodes one level at a time, from left to right. A null value marks a missing child.
In a binary search tree, the value of each node is larger than all values in its left subtree. It is smaller than all values in its right subtree. The subtree of a node is that node and all nodes below it.
Delete the node whose value is key, if that node exists. The nodes that remain must also form a binary search tree. Return the root of the new tree.
Many trees can be correct. The checker accepts any binary search tree that has the correct set of values.
Example 1
- Input:
- root = [8,3,12,1,5,10,14], key = 3
- Output:
- [8,5,12,1,null,10,14]
- Explanation:
- Node 3 has two children. This answer puts 5, the smallest value in the right subtree of node 3, in its place.
Example 2
- Input:
- root = [8,3,12,1,5,10,14], key = 9
- Output:
- [8,3,12,1,5,10,14]
- Explanation:
- No node has the value 9, so the tree does not change.
Example 3
- Input:
- root = [6,null,9], key = 6
- Output:
- [9]
- Explanation:
- The root has only one child, so that child becomes the new root.
Constraints
- 0 <= number of nodes <= 10^4
- -10^5 <= Node.val <= 10^5
- All node values are unique.
- root is a valid binary search tree.
- -10^5 <= key <= 10^5
Follow-up
Delete the node in O(h) time, where h is the height of the tree.
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
Sources · 1 reference
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 2 records
LinkedIn
Leetcode 450. Delete Node in a BST
Reported Feb 24, 2025 · 1 source report
MID_LEVEL
Source snapshot: Nov 12, 2026
View sourcePinterest
Leetcode 450. Delete Node in a BST
Reported May 19, 2025 · 1 source report
SENIOR
Source snapshot: Nov 12, 2026
View source