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:
Follow up: Could you solve it with time complexity O(height of tree)?
Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.

The tree does not contain a node with value = 0.
[0, 10^4].-10^5 <= Node.val <= 10^5root is a valid binary search tree.-10^5 <= key <= 10^5No developer has claimed the Python throne for LeetCode #450 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
# 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 root is None:
return None
if root.val == key:
if root.right: # If root has right child, then use the right child to replace root.
child = root.right
# Keypoint. Move `root.left` to the leftest leaf of root.right
while child.left:
child = child.left
child.left = root.left
return root.right
else: # If root doesn't have right child, then use the left child to replace root.
return root.left
elif root.val > key:
root.left = self.deleteNode(root.left, key)
else:
root.right = self.deleteNode(root.right, key)
return root
# 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]:
dummy = TreeNode(-1) # key 1. Having a dummy is to deal with the condition of `root.val == key`.
dummy.left = root
node = root # `node` is one `val == key`.
parent = (dummy, True) # True means `is left`.
found = False
while node and not found:
if node.val == key:
found = True
elif node.val > key:
parent = (node, True) # True means `is left`.
node = node.left
else:
parent = (node, False) # False means `is right`.
node = node.right
if not found:
return root
child_node = node.right or node.left # Prefer using `node.right` to replace `node`.
is_left = parent[1]
if is_left:
parent[0].left = child_node # replace
else:
parent[0].right = child_node # replace
if node.right and node.left:
# move `node.left` to the left of leftest child of `node.right`.
while child_node.left:
child_node = child_node.left
child_node.left = node.left
return dummy.left
No developer has claimed the Java throne for LeetCode #450 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the C++ throne for LeetCode #450 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Javascript throne for LeetCode #450 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Go throne for LeetCode #450 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Rust throne for LeetCode #450 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the C# throne for LeetCode #450 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Ruby throne for LeetCode #450 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Swift throne for LeetCode #450 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Kotlin throne for LeetCode #450 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.