Leetcode 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

解题思路:删除二叉查找树上的节点,要考虑到几种情况:
1)待删除的节点包含有左,右子树,那删除的时候,要找到后继节点,这个后继节点只可能在删除节点的右子树找。
2)待删除的节点只包含有一个子树,直接拿父亲节点与删除节点的子节点相连
3)待删除节点是叶节点,那原来相连的节点就得赋为空
  1 #include <stdio.h>
  2 
  3 struct TreeNode {
  4     int val;
  5     TreeNode *left;
  6     TreeNode *right;
  7     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8 };
  9 
 10 TreeNode* BST_search(TreeNode *node, int value, TreeNode *&parent){
 11     while(node){
 12         if (node->val == value){
 13             break;
 14         }
 15         parent = node;
 16         if (value < node->val){
 17             node = node->left;
 18         }
 19         else{
 20             node = node->right;
 21         }
 22     }
 23     return node;
 24 }
 25 
 26 void delete_node(TreeNode *parent, TreeNode *node){
 27     if (node->val < parent->val){
 28         if (node->left && !node->right){
 29             parent->left = node->left;
 30         }
 31         else if (!node->left && node->right){
 32             parent->left = node->right;
 33         }
 34         else{
 35             parent->left = NULL;
 36         }
 37     }
 38     else if(node->val > parent->val){
 39         if (node->left && !node->right){
 40             parent->right = node->left;
 41         }
 42         else if (!node->left && node->right){
 43             parent->right = node->right;
 44         }
 45         else{
 46             parent->right = NULL;
 47         }
 48     }
 49 }
 50 
 51 TreeNode* find_successor(TreeNode *node, TreeNode *&parent){
 52     parent = node;
 53     TreeNode *ptr = node->right;
 54     while(ptr->left){
 55         parent = ptr;
 56         ptr = ptr->left;
 57     }
 58     return ptr;
 59 }
 60 
 61 class Solution {
 62 public:
 63     TreeNode* deleteNode(TreeNode* root, int key) {
 64         TreeNode *parent = NULL;
 65         TreeNode *node = BST_search(root, key, parent);
 66         if (!node){
 67             return root;
 68         }
 69         if (node->left && node->right){
 70             TreeNode *successor = find_successor(node, parent);
 71             delete_node(parent, successor);
 72             node->val = successor->val;
 73             return root;
 74         }
 75         if (parent){
 76                delete_node(parent, node);
 77                return root;
 78         }
 79            if (node->left){
 80                root = node->left;
 81         }
 82         else{
 83                root = node->right;
 84         }
 85         return root;
 86     }
 87 };
 88 
 89 void preorder_print(TreeNode *node,int layer){
 90     if (!node){
 91         return;
 92     }
 93     for (int i = 0; i < layer; i++){
 94         printf("-----");
 95     }
 96     printf("[%d]
", node->val);
 97     preorder_print(node->left, layer + 1);
 98     preorder_print(node->right, layer + 1);
 99 }
100 
101 int main(){
102     for (int i = 1; i <= 7; i++){
103         printf("key = %d
", i);
104         TreeNode a(5);
105         TreeNode b(3);
106         TreeNode c(6);
107         TreeNode d(2);
108         TreeNode e(4);
109         TreeNode f(7);
110         a.left = &b;
111         a.right = &c;
112         b.left = &d;
113         b.right = &e;
114         c.right = &f;
115         Solution solve;
116         TreeNode *root = solve.deleteNode(&a, i);
117         preorder_print(root, 0);
118         printf("
");
119     }
120     return 0;
121 }

原文地址:https://www.cnblogs.com/Hwangzhiyoung/p/8733611.html