filmov
tv
🔍 #DeleteNode in #BST | Python Solution with Inorder Successor #LeetCode75 #BinaryTree #PythonCode

Показать описание
🚀 Deleting a Node in a Binary Search Tree (BST) - Python Solution
In this problem, we are given the root of a Binary Search Tree (BST) and a key. Our task is to find the node with the given value and delete it from the tree while maintaining the BST properties. If the node does not exist, we simply return the original tree.
🛠 Approach & Solution Explanation
🔹 Step 1: Search for the Node to be Deleted
Since the tree is a BST, we utilize its properties for efficient searching:
🔹 Step 2: Handle Three Cases for Deletion
1️⃣ Node is a Leaf Node (No Children)
Simply remove the node by returning None.
2️⃣ Node has One Child (Left or Right)
Replace the node with its only child.
3️⃣ Node has Two Children
Find the inorder successor (smallest node in the right subtree).
Replace the node's value with the successor's value.
Delete the successor node.
🔹 Step 3: Finding the Inorder Successor
The inorder successor is the leftmost node in the right subtree.
We move left repeatedly until we find the smallest value.
💡 Python Implementation
class TreeNode:
def __init__(self, val=0, left=None, right=None):
class Solution:
def deleteNode(self, root, key):
if not root:
return None # If the tree is empty or key is not found, return None
# Step 1: Find the node to delete
else:
# Step 2: Node found, handle deletion cases
# Case 1: Node has no children (leaf node)
return None
# Case 2: Node has only one child
# Case 3: Node has two children
return root # Return the updated root
def findMin(self, node):
return node
🔹 Complexity Analysis
✅ Time Complexity: O(height of tree) → O(log N) for balanced BST, O(N) for skewed BST.
✅ Space Complexity: O(1) for iterative, O(log N) for recursive calls.
🔹 Example Walkthrough
Example 1:
Input:
5
/ \
3 6
/ \ \
2 4 7
key = 3
Steps:
1️⃣ Find 3 (exists).
2️⃣ Node 3 has two children (2, 4).
3️⃣ Find inorder successor → 4.
4️⃣ Replace 3 with 4.
5️⃣ Delete 4 (which had no children).
Output:
5
/ \
4 6
/ \
2 7
Example 2: (Deleting a non-existent node)
Input: [5,3,6,2,4,null,7], key = 0
📌 Output: Same tree, as 0 is not present.
Example 3: (Empty Tree)
Input: [], key = 0
📌 Output: []
🚀 Key Takeaways
✅ Uses BST properties for efficient searching (O(log N)).
✅ Handles all three cases of deletion properly.
✅ Finds and replaces with inorder successor for O(log N).
🔹 Follow-up
To optimize for height-balanced BSTs, consider AVL trees or Red-Black Trees for automatic balancing after deletion.
Would you like a solution for balancing the BST post-deletion? 😊🚀
In this problem, we are given the root of a Binary Search Tree (BST) and a key. Our task is to find the node with the given value and delete it from the tree while maintaining the BST properties. If the node does not exist, we simply return the original tree.
🛠 Approach & Solution Explanation
🔹 Step 1: Search for the Node to be Deleted
Since the tree is a BST, we utilize its properties for efficient searching:
🔹 Step 2: Handle Three Cases for Deletion
1️⃣ Node is a Leaf Node (No Children)
Simply remove the node by returning None.
2️⃣ Node has One Child (Left or Right)
Replace the node with its only child.
3️⃣ Node has Two Children
Find the inorder successor (smallest node in the right subtree).
Replace the node's value with the successor's value.
Delete the successor node.
🔹 Step 3: Finding the Inorder Successor
The inorder successor is the leftmost node in the right subtree.
We move left repeatedly until we find the smallest value.
💡 Python Implementation
class TreeNode:
def __init__(self, val=0, left=None, right=None):
class Solution:
def deleteNode(self, root, key):
if not root:
return None # If the tree is empty or key is not found, return None
# Step 1: Find the node to delete
else:
# Step 2: Node found, handle deletion cases
# Case 1: Node has no children (leaf node)
return None
# Case 2: Node has only one child
# Case 3: Node has two children
return root # Return the updated root
def findMin(self, node):
return node
🔹 Complexity Analysis
✅ Time Complexity: O(height of tree) → O(log N) for balanced BST, O(N) for skewed BST.
✅ Space Complexity: O(1) for iterative, O(log N) for recursive calls.
🔹 Example Walkthrough
Example 1:
Input:
5
/ \
3 6
/ \ \
2 4 7
key = 3
Steps:
1️⃣ Find 3 (exists).
2️⃣ Node 3 has two children (2, 4).
3️⃣ Find inorder successor → 4.
4️⃣ Replace 3 with 4.
5️⃣ Delete 4 (which had no children).
Output:
5
/ \
4 6
/ \
2 7
Example 2: (Deleting a non-existent node)
Input: [5,3,6,2,4,null,7], key = 0
📌 Output: Same tree, as 0 is not present.
Example 3: (Empty Tree)
Input: [], key = 0
📌 Output: []
🚀 Key Takeaways
✅ Uses BST properties for efficient searching (O(log N)).
✅ Handles all three cases of deletion properly.
✅ Finds and replaces with inorder successor for O(log N).
🔹 Follow-up
To optimize for height-balanced BSTs, consider AVL trees or Red-Black Trees for automatic balancing after deletion.
Would you like a solution for balancing the BST post-deletion? 😊🚀