🧠
Brute Force (Recursive Search and Delete)
💡 This approach introduces the fundamental recursive logic to find and delete nodes in BST. It helps beginners understand the problem structure before optimizing.
Intuition
Recursively search for the node to delete. Once found, handle three cases: leaf node, node with one child, and node with two children by replacing with inorder successor.
Algorithm
- If root is null, return null.
- If key is less than root's value, recurse on left subtree.
- If key is greater than root's value, recurse on right subtree.
- If key equals root's value, handle deletion:
- - If node is leaf, return null.
- - If node has one child, return that child.
- - If node has two children, find inorder successor, replace node's value, and delete successor recursively.
💡 The challenge is to carefully handle each deletion case and maintain BST properties during recursion.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def deleteNode(root, key):
if not root:
return None
if key < root.val:
root.left = deleteNode(root.left, key)
elif key > root.val:
root.right = deleteNode(root.right, key)
else:
if not root.left:
return root.right
elif not root.right:
return root.left
else:
successor = root.right
while successor.left:
successor = successor.left
root.val = successor.val
root.right = deleteNode(root.right, successor.val)
return root
# Example usage:
# Construct BST and call deleteNode(root, key)
Line Notes
if not root:Base case: if tree/subtree is empty, nothing to delete
if key < root.val:Search left subtree if key is smaller
elif key > root.val:Search right subtree if key is larger
else:Found node to delete
if not root.left:If no left child, replace node with right child
elif not root.right:If no right child, replace node with left child
successor = root.rightFind inorder successor in right subtree
while successor.left:Go as left as possible to find smallest node
root.val = successor.valReplace current node's value with successor's value
root.right = deleteNode(root.right, successor.val)Delete successor node recursively
class TreeNode {
int val;
TreeNode left, right;
TreeNode(int x) { val = x; }
}
public class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
if (root == null) return null;
if (key < root.val) {
root.left = deleteNode(root.left, key);
} else if (key > root.val) {
root.right = deleteNode(root.right, key);
} else {
if (root.left == null) return root.right;
else if (root.right == null) return root.left;
else {
TreeNode successor = root.right;
while (successor.left != null) successor = successor.left;
root.val = successor.val;
root.right = deleteNode(root.right, successor.val);
}
}
return root;
}
// Example main method to test
public static void main(String[] args) {
// Build BST and call deleteNode
}
}
Line Notes
if (root == null) return null;Base case: empty subtree
if (key < root.val)Traverse left subtree if key smaller
else if (key > root.val)Traverse right subtree if key larger
else {Found node to delete
if (root.left == null) return root.right;No left child, replace with right
else if (root.right == null) return root.left;No right child, replace with left
TreeNode successor = root.right;Find inorder successor
while (successor.left != null) successor = successor.left;Go left to find smallest node
root.val = successor.val;Replace value with successor's
root.right = deleteNode(root.right, successor.val);Delete successor recursively
#include <iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode *left, *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
TreeNode* deleteNode(TreeNode* root, int key) {
if (!root) return NULL;
if (key < root->val) {
root->left = deleteNode(root->left, key);
} else if (key > root->val) {
root->right = deleteNode(root->right, key);
} else {
if (!root->left) return root->right;
else if (!root->right) return root->left;
else {
TreeNode* successor = root->right;
while (successor->left) successor = successor->left;
root->val = successor->val;
root->right = deleteNode(root->right, successor->val);
}
}
return root;
}
// Example usage in main
int main() {
// Build BST and call deleteNode
return 0;
}
Line Notes
if (!root) return NULL;Base case: empty subtree
if (key < root->val)Traverse left subtree if key smaller
else if (key > root->val)Traverse right subtree if key larger
else {Found node to delete
if (!root->left) return root->right;No left child, replace with right
else if (!root->right) return root->left;No right child, replace with left
TreeNode* successor = root->right;Find inorder successor
while (successor->left) successor = successor->left;Go left to find smallest node
root->val = successor->val;Replace value with successor's
root->right = deleteNode(root->right, successor->val);Delete successor recursively
function TreeNode(val, left=null, right=null) {
this.val = val;
this.left = left;
this.right = right;
}
function deleteNode(root, key) {
if (!root) return null;
if (key < root.val) {
root.left = deleteNode(root.left, key);
} else if (key > root.val) {
root.right = deleteNode(root.right, key);
} else {
if (!root.left) return root.right;
else if (!root.right) return root.left;
else {
let successor = root.right;
while (successor.left) successor = successor.left;
root.val = successor.val;
root.right = deleteNode(root.right, successor.val);
}
}
return root;
}
// Example usage:
// let root = new TreeNode(...);
// console.log(deleteNode(root, key));
Line Notes
if (!root) return null;Base case: empty subtree
if (key < root.val)Traverse left subtree if key smaller
else if (key > root.val)Traverse right subtree if key larger
else {Found node to delete
if (!root.left) return root.right;No left child, replace with right
else if (!root.right) return root.left;No right child, replace with left
let successor = root.right;Find inorder successor
while (successor.left) successor = successor.left;Go left to find smallest node
root.val = successor.val;Replace value with successor's
root.right = deleteNode(root.right, successor.val);Delete successor recursively
TimeO(h) where h is the height of the BST
SpaceO(h) due to recursion stack
We traverse down the tree once to find the node, which takes O(h). Deletion and successor search also take O(h) in worst case.
💡 For a balanced BST with n=10^5, height h ~ 17, so about 17 recursive calls and operations.
Interview Verdict: Accepted
This approach is efficient for balanced BSTs and is the standard recursive solution interviewers expect.