You are given a root node reference of a BST and a key, delete the node with the given key in the BST, if present. Return the root node reference (possibly updated) of the BST.
Basically, the deletion can be divided into two stages:
Note: There can be multiple results after deleting the node, return any one of them.
Example 1:
Input: root = [5,3,9,1,4], key = 3
Output: [5,4,9,1]Explanation: Another valid answer is:
Example 2:
Input: root = [5,3,6,null,4,null,10,null,null,7], key = 3
Output: [5,4,6,null,null,null,10,7]Constraints:
0 <= The number of nodes in the tree <= 10,000.-100,000 <= key, Node.val <= 100,000Node.val are unique.# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
if not root:
return root
if key > root.val:
root.right = self.deleteNode(root.right, key)
elif key < root.val:
root.left = self.deleteNode(root.left, key)
else:
if not root.left:
return root.right
elif not root.right:
return root.left
cur = root.right
while cur.left:
cur = cur.left
root.val = cur.val
root.right = self.deleteNode(root.right, root.val)
return rootWhere is the height of the given binary search tree.
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
if not root:
return root
if key > root.val:
root.right = self.deleteNode(root.right, key)
elif key < root.val:
root.left = self.deleteNode(root.left, key)
else:
if not root.left:
return root.right
elif not root.right:
return root.left
cur = root.right
while cur.left:
cur = cur.left
cur.left = root.left
res = root.right
del root
return res
return rootWhere is the height of the given binary search tree.
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
if not root:
return root
parent = None
cur = root
# Find the node to delete
while cur and cur.val != key:
parent = cur
if key > cur.val:
cur = cur.right
else:
cur = cur.left
if not cur:
return root
# Node with only one child or no child
if not cur.left or not cur.right:
child = cur.left if cur.left else cur.right
if not parent:
return child
if parent.left == cur:
parent.left = child
else:
parent.right = child
else:
# Node with two children
par = None # parent of right subTree min node
delNode = cur
cur = cur.right
while cur.left:
par = cur
cur = cur.left
if par: # if there was a left traversal
par.left = cur.right
cur.right = delNode.right
cur.left = delNode.left
if not parent: # if we're deleting root
return cur
if parent.left == delNode:
parent.left = cur
else:
parent.right = cur
return rootWhere is the height of the given binary search tree.