Delete a Node in a Binary Search Tree

viaLeetCode

Problem: Given the root of a Binary Search Tree (BST) and a key value, delete the node with that key from the BST and return the new root, maintaining BST properties.

Constraints: Tree may have up to 10^4 nodes; the key may or may not exist in the tree.

Example: root = [5,3,6,2,4,null,7], key = 3 -> one valid resulting BST is [5,4,6,2,null,null,7].

Approach: Recursively search for the node. If found: if it has no children, remove it directly; if it has one child, replace it with that child; if it has two children, replace its value with the in-order successor (or predecessor) and recursively delete that successor from the right subtree.

Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account