450. Delete Node in a BST

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
class Solution {
     public TreeNode deleteNode(TreeNode root, int key) {
        if(root == null) {
            return root;
        }
        
        //find the target
        if(root.val < key) {
            root.right = deleteNode(root.right, key);
            return root;
        } else if(root.val > key) {
            root.left = deleteNode(root.left, key);
            return root;
        }
        
        //No child or only one child
        if(root.left == null && root.right == null) {
            return null;
        } else if(root.left == null) {
            return root.right;
        } else if(root.right == null) {
            return root.left;
        }
        
        //Two children
        if(root.right.left == null) {
            root.right.left = root.left;
            return root.right;
        } else {
            TreeNode smallest = deleteSmallest(root.right);
            smallest.left = root.left;
            smallest.right = root.right;
            return smallest;
        }
    }
    
    private TreeNode deleteSmallest(TreeNode root) {
        TreeNode cur = root.left;
        TreeNode pre = root;
        while(cur.left != null) {
            pre = cur;
            cur = cur.left;
        }
        pre.left = cur.right;
        return cur;
    }
}

 删除bst的一个node,会引出很多问题,

1.如何确定要删除的node在哪:用bst的性质,如果key比root大,说明在root.right, 因为我们要在tree上操作,所以递归使root.right = func(root.right, key), 最后root。right就是排好序并删除了key node的,随后返回root。

如果key比root小,反之。

2.这时我们到了要删除的node,如何处理它的child,我们分为两种情况:1. 没有child(直接返回null,这时上层root.left/right == null,说明删除了这个node),或者只有left或者right child,我们只需要返回root.left(当只有left child)或root.right(当只有right child),说明把root删掉了,返回剩下的它的childs)

2.2 有两个child,也需要判断,因为我们删除了这个root后少了主心骨,要从孩子中选一个来代替,作为新的root传上去。那究竟传哪一个呢?因为bst是right > root > left, 新的root要起到表率作用,既要大于所有的left又要小于所有的right,所以我们从right里面选,选谁呢?选right child中最小的,然而最小的也比你所有left child大。再写一个method,pre是root,cur是root.left, 这么left下去直到左边没东西了,就找到了最小的。因为我们要返回他,所以也要安排好他的左右child,而它现在只有right child,所以让pre.left = cur.right. 好的,我们得到了smallest干什么呢?做为新的root阿,所以要把刚才的left right child安顿好,smallest.left = root.left, smallest.right = root.right, 这样就把原来的root删除了,返回smallest。

然而,如果root.right.left == null 时,说明root.right已经是smallest了,就让root.right.left = root.left, 然后返回root.right。

这道题考bst的性质,考recursive害挺有意义

posted @ 2020-06-04 11:46  Schwifty  阅读(137)  评论(0)    收藏  举报