Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Delete Node in a BST problem solution

YASH PAL, 31 July 202422 January 2026

In this Leetcode Delete Node in a BST problem solution, we have given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST.

Basically, the deletion can be divided into two stages:

  1. Search for a node to remove.
  2. If the node is found, delete the node.
Leetcode Delete Node in a BST problem solution

Leetcode Delete Node in a BST problem solution in Python.

class Solution:
    def deleteNode(self, root: TreeNode, key: int) -> TreeNode:
        if not root:
            return root
        if root.val > key:
            root.left = self.deleteNode(root.left, key)
            return root
        elif root.val < key:
            root.right = self.deleteNode(root.right, key)
            return root
        else: # root.val == key
            if not root.right:
                return root.left
            smInRight = root.right
            # Find the smallest node of right subtree of root 
            # and attach left subtree of root to left of the smallest node in right
            while smInRight.left: 
                smInRight = smInRight.left
            smInRight.left = root.left
            return root.right

Delete Node in a BST problem solution in Java.

public TreeNode deleteNode(TreeNode root, int key) {
    if(root == null)
        return root;

    if(root.val == key){
        if(root.right == null)
            return root.left;

        TreeNode curr = root.right;
        while(curr.left != null)
            curr = curr.left;
        curr.left = root.left;

        return root.right;
    }else if(root.val < key){
        root.right = deleteNode(root.right, key);
    }else{
        root.left = deleteNode(root.left, key);
    }
    return root;
}

Problem solution in C++.

class Solution {
public:
    TreeNode * deleteNode(TreeNode*& root, int key) {
        if (!root) {
            return NULL;
        }

        if (root->val == key) {
            if (root->right) {
                TreeNode* nextGt = NULL;
                findMin(root->right, nextGt);
                swap(root->val, nextGt->val);
                root->right = deleteNode(root->right, key);
                return root;
            }
            else {
                TreeNode* res = root->left;
                delete root;
                return res;
            }
        }
        else if (root->val < key) {
            root->right = deleteNode(root->right, key);
            return root;
        }
        else if (root->val > key) {
            root->left = deleteNode(root->left, key);
            return root;
        }
        else {
            return root;
        }
    }
private:
    void findMin(TreeNode* root, TreeNode*& min) {
        if(!root){
            return;
        }
        
        min = root;
        while (min && min->left) {
            min = min->left;
        }
    }
};

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes