Leetcode Tag Search | Back

450. Delete Node in a BST

Category: /leetcode

Leetcode Link | Tree template

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:

Search for a node to remove. If the node is found, delete the node. Follow up: Can you solve it with time complexity O(height of tree)?

Solution:

  def successor(self, root):
    root = root.right
    while root.left:
      root = root.left
    return root.val

  def predecessor(self, root):
    root = root.left
    while root.right:
      root = root.right
    return root.val

  def deleteNode(self, root: TreeNode, key: int) -> TreeNode:
    if not root:
      return None

    # delete from the right subtree
    if key > root.val:
      root.right = self.deleteNode(root.right, key)
    elif key < root.val:
      root.left = self.deleteNode(root.left, key)
    else: # found the node
      if not (root.left or root.right):
        # the node is a leaf
        root = None
      elif root.right:
        root.val = self.successor(root)
        root.right = self.deleteNode(root.right, root.val)
      else:
        root.val = self.predecessor(root)
        root.left = self.deleteNode(root.left, root.val)
    return root

讨论

提示

  • 如果看不到讨论部分, 请暂时关掉adblock in Firefox/Chrome
  • 本网站使用Javascript实现评论功能, 此处外链对提高您的网站PR没有帮助. (潜台词: 请不要灌水, 谢谢)