bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 450. Delete Node in a BST

450. Delete Node in a BST

Leetcode Solutions

Delete Node in a BST

  1. If the tree is empty, return null.
  2. If the key to be deleted is greater than the root's key, recurse on the right subtree.
  3. If the key to be deleted is less than the root's key, recurse on the left subtree.
  4. 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.
  5. Return the root of the modified BST.
UML Thumbnail

Delete Node in a BST using Iterative Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...