End of Year Sale: Use Coupon Code END2025 to Get Extra 25% Off.
12DAYS
:
11HOURS
:
46MINUTES
:
36SECONDS
Leetcode Problem 450. Delete Node in a BST
450. Delete Node in a BST
Leetcode Solutions
Delete Node in a BST
If the tree is empty, return null.
If the key to be deleted is greater than the root's key, recurse on the right subtree.
If the key to be deleted is less than the root's key, recurse on the left subtree.
If the key is equal to the root's key, then the node to be deleted is found:
a. If the node is a leaf, delete it by setting it to null.
b. If the node has a right child, find the successor, replace the node's value with the successor's value, and delete the successor.
c. If the node has only a left child, find the predecessor, replace the node's value with the predecessor's value, and delete the predecessor.