450. Delete Node in a BST

Tags: [BST], [classification-discussion], [implementation]

Link: https://leetcode.com/problems/delete-node-in-a-bst/?tab=Description

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.

Note:Time complexity should be O(height of tree).

Example:

root = [5,3,6,2,4,null,7]
key = 3

    5
   / \
  3   6
 / \   \
2   4   7

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 following BST.

    5
   / \
  4   6
 /     \
2       7

Another valid answer is [5,2,6,null,4,null,7].

    5
   / \
  2   6
   \   \
    4   7

Solution: classification discussion

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def deleteNode(self, root, key):
        """
        :type root: TreeNode
        :type key: int
        :rtype: TreeNode
        """
        if not root or key is None:
            return root

        parent = None
        curr = root
        while curr:
            if curr.val == key:
                return self.delete_node_helper(root, parent, curr)
            elif curr.val > key:
                parent = curr
                curr = curr.left
            else:
                parent = curr
                curr = curr.right

        return root

    def delete_node_helper(self, root, parent, curr):
        if not parent:
            # curr is the root of the tree
            if curr.left:
                right_most_node = self.get_right_most_node(curr.left)
                right_most_node.right = curr.right
                return curr.left
            else:
                return curr.right
        else:
            if curr == parent.left:
                if curr.left:
                    right_most_node = self.get_right_most_node(curr.left)
                    right_most_node.right = curr.right
                    parent.left = curr.left
                else:
                    parent.left = curr.right
                return root
            else:
                if curr.left:
                    right_most_node = self.get_right_most_node(curr.left)
                    right_most_node.right = curr.right
                    parent.right = curr.left
                else:
                    parent.right = curr.right
                return root

    def get_right_most_node(self, curr):
        tmp = curr
        while tmp.right:
            tmp = tmp.right
        return tmp

Revelation:

  • For the problem about the operation on the tree, we can try to use the 'classification discussion' to solve problem.
  • For the problem about the operation on the tree, it's better we can keep trace the three points: parent, curr.left, curr.right.

Note:

  • Time complexity = O(lgn), n is the numbers of nodes from the given tree.
  • The height of the binary tree is lgn, n is the numbers of the nodes of the given tree.

results matching ""

    No results matching ""