Arena / Problems /

450. Delete Node in a BST

Medium 0/10 Thrones Claimed
Prev Next

Problem Specification

LeetCode #450

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.

Follow up: Could you solve it with time complexity O(height of tree)?

Example 1
Input: root = [5,3,6,2,4,null,7], key = 3
Output: [5,4,6,2,null,null,7]
Explanation:

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.

Example 2
Input: root = [5,3,6,2,4,null,7], key = 0
Output: [5,3,6,2,4,null,7]
Explanation:

The tree does not contain a node with value = 0.

Example 3
Input: root = [], key = 0
Output: []
Constraints
  • The number of nodes in the tree is in the range [0, 10^4].
  • -10^5 <= Node.val <= 10^5
  • Each node has a unique value.
  • root is a valid binary search tree.
  • -10^5 <= key <= 10^5
Topics & Algorithms
⚔️

Vacant Python Throne

No 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.

Contenders & Community Submissions (2)

Peer solutions & challenges
#1 Recursion by Anonymous Developer
0
# 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
#2 Iteration by Anonymous Developer
0
# 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
⚔️

Vacant Java Throne

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.

⚔️

Vacant C++ Throne

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.

⚔️

Vacant Javascript Throne

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.

⚔️

Vacant Go Throne

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.

⚔️

Vacant Rust Throne

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.

⚔️

Vacant C# Throne

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.

⚔️

Vacant Ruby Throne

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.

⚔️

Vacant Swift Throne

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.

⚔️

Vacant Kotlin Throne

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.