Delete Node in a BST
Given the root of a binary search tree and a key, delete the node with that key (if it exists) and return the root of the modified BST. The result must remain a valid BST.
Open official problem prompt ↗Remove one keyed node from a BST while keeping every remaining node in sorted order.
Removing a manager from an org chart: if they have one report, that report moves up; if they have two teams, promote the most-junior person from the right team into the vacant seat.
- Input
- root = [5,3,6,2,4,null,7], key = 3
- Output
- [5,4,6,2,null,null,7]
- Why
- Node 3 has two children, so it is replaced by its in-order successor 4, and the duplicate 4 is removed from the right subtree.
The number of nodes is in the range [0, 10^4]-10^5 <= Node.val <= 10^5Each node has a unique valueroot is a valid binary search tree-10^5 <= key <= 10^5