450. Delete Node in a BST
Delete Node in a BST (LeetCode 450) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Tree Traversal Patterns (DFS & BFS) pattern, in the Recursive Inorder Traversal subpattern.
How to solve Delete Node in a BST
Delete Node in a BST is a Inorder Traversal problem, part of Tree Traversal Patterns (DFS & BFS). Master inorder traversal for BST validation and kth element problems. Produces sorted order in BST.
Work towards the solution in these steps:
- Use the ordering property of a binary search tree to decide whether to continue into the left or right subtree.
- When the target has at most one child, its replacement is immediate: the child, or null if there is no child.
- For a target with two children, choose either the next larger value or the next smaller value, copy it into the target, and delete that replacement from its original position.
The reference solution runs in O(h) time and O(h) space.
The Delete Node in a BST solution and editorial walks through the full approach with reference code.
Problem statement
Given root, the root of a binary search tree, and key, remove the node whose value equals key and return the root of the resulting tree. A binary search tree is a binary tree where every value in a node's left subtree is smaller than the node's value, and every value in its right subtree is larger.
If the node to remove has no children, remove it directly. If it has one child, connect its parent to that child. If it has two children, replace its value with a suitable value from one subtree—such as the smallest value in its right subtree or the largest value in its left subtree—and then remove that replacement node.
All values are unique. The returned tree must be a valid binary search tree containing exactly the original values except key. If key does not occur in the tree, return the tree unchanged. The returned value is the new root; it may be null when the resulting tree is empty.
Example 1:
Input: root = [8,3,10,1,6,null,14,null,null,4,7,13], key = 3 Output: [8,4,10,1,6,null,14,null,null,null,7,13] Explanation: The node 3 has two children. Replacing it with its in-order successor 4 and removing the original 4 preserves the search-tree ordering.
Example 2:
Input: root = [5,2,9,null,3,7,12], key = 12 Output: [5,2,9,null,3,7] Explanation: The target is a leaf, so it can be removed without changing any other links.
Example 3:
Input: root = [6,4,11,2,5,8,13], key = 10 Output: [6,4,11,2,5,8,13] Explanation: No node has value 10, so the original tree is returned unchanged.
Constraints:
- 0 <= number of nodes in root <= 5000 - -10^5 <= node values <= 10^5 - -10^5 <= key <= 10^5 - All node values in root are unique - root represents a valid binary search tree - The tree is encoded in level order, using null for missing children
Topics and companies
Topics: Binary Search Tree, Binary Tree, Tree, Recursion.
How to practise Delete Node in a BST on Thita.ai
Starter code is provided in C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(h) time and O(h) space.
Open Delete Node in a BST in the code editor, or read the Delete Node in a BST editorial for a worked solution with its approach and complexity analysis.
Where Delete Node in a BST sits in the DSA pattern sheet
- Tree Traversal Patterns (DFS & BFS) pattern guide
- Recursive Inorder Traversal subpattern
- The full DSA patterns sheet
Problems related to Delete Node in a BST
Other problems that use the same Recursive Inorder Traversal and Tree Traversal Patterns (DFS & BFS) techniques:
- Binary Search Tree Iterator — Medium, same subpattern
- Binary Tree Inorder Traversal — Easy, same subpattern
- Find Mode in Binary Search Tree — Easy, same subpattern
- Kth Smallest Element in a BST — Medium, same subpattern
- Minimum Absolute Difference in BST — Easy, same subpattern
- Validate Binary Search Tree — Medium, same subpattern
Delete Node in a BST FAQ
What is the LeetCode number of Delete Node in a BST?
Delete Node in a BST is LeetCode problem 450, rated Medium.
What pattern is used to solve Delete Node in a BST?
Delete Node in a BST is a Inorder Traversal problem. It belongs to the Tree Traversal Patterns (DFS & BFS) pattern, in the Recursive Inorder Traversal subpattern.
What is the time complexity of Delete Node in a BST?
The reference solution runs in O(h) time and O(h) space.