15% off all plans & roadmaps Use code
See plansProblem editorial
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.
Delete Node in a BST is a Tree Traversal Patterns (DFS & BFS) problem, in the Recursive Inorder Traversal subpattern. The pattern covers preorder, inorder, level order traversal, LCA, and serialization
The reference solution runs in O(h), where h is the height of the tree; O(n) in the worst case. time and O(h) for recursive call stack; O(n) in the worst case. space.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Delete Node in a BST is a Tree Traversal Patterns (DFS & BFS) problem, in the Recursive Inorder Traversal subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.