LeetCode 450. Delete Node in a BST

原题链接在这里: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

题解:

根据BST的特性找key的TreeNode node. 然后delete掉. 有几种情况:

找不到, return null.

node.left == null, return node.right.

node.right == null, return node.left.

左右都不为空,找node的successor, swap value, 然后delete掉successor.

Time Complexity: O(h). Space: O(h), stack sapce.

AC Java:

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     public TreeNode deleteNode(TreeNode root, int key) {
12         if(root == null){
13             return root;
14         }
15         if(key < root.val){
16             root.left = deleteNode(root.left, key);
17         }else if(key > root.val){
18             root.right = deleteNode(root.right, key);
19         }else{
20             if(root.left == null){
21                 return root.right;
22             }else if(root.right == null){
23                 return root.left;
24             }else{
25                 TreeNode suc = findSuc(root.right);
26                 root.val = suc.val;
27                 root.right = deleteNode(root.right, suc.val);
28             }
29         }
30         return root;
31     }
32     
33     private TreeNode findSuc(TreeNode root){
34         while(root.left != null){
35             root = root.left;
36         }
37         return root;
38     }
39 }

Iteration Method. 

在找key node时用pre来maintain 之前的位置, 若是找到的cur是pre的left, pre的left就更新为去掉cur后的子树. 反之亦然.

delete node时若是该node左右子树都不为null, 找successor然后swap. 返回successor.

Time Complexity: O(h). Space: O(1).

AC Java:

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     public TreeNode deleteNode(TreeNode root, int key) {
12         if(root == null){
13             return root;
14         }
15         TreeNode cur = root;
16         TreeNode pre = null;
17         while(cur!=null && cur.val!=key){
18             pre = cur;
19             if(key < cur.val){
20                 cur = cur.left;
21             }else if(key > cur.val){
22                 cur = cur.right;
23             }
24         }
25         if(pre == null){
26             return delete(cur);
27         }
28         if(pre.left == cur){
29             pre.left = delete(cur);
30         }else{
31             pre.right = delete(cur);
32         }
33         return root;
34     }
35     
36     private TreeNode delete(TreeNode root){
37         if(root == null){
38             return null;
39         }else if(root.left == null){
40             return root.right;
41         }else if(root.right == null){
42             return root.left;
43         }
44         TreeNode suc = root.right;
45         TreeNode pre = null;
46         while(suc.left != null){
47             pre = suc;
48             suc = suc.left;
49         }
50         suc.left = root.left;
51         if(root.right != suc){
52             pre.left = suc.right;
53             suc.right = root.right;
54         }
55         return suc;
56     }
57 }

类似Split BST.

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/6484519.html