zoukankan      html  css  js  c++  java
  • 【Lintcode】087.Remove Node in Binary Search Tree

    题目:

    Given a root of Binary Search Tree with unique value for each node. Remove the node with given value. If there is no such a node with given value in the binary search tree, do nothing. You should keep the tree still a binary search tree after removal.

    Given binary search tree:

        5
       / 
      3   6
     / 
    2   4
    

    Remove 3, you can either return:

        5
       / 
      2   6
       
        4
    

    or

        5
       / 
      4   6
     /
    2
    

     

    题解:

      这个题就是考察对二叉树操作的熟练程度,没有多少技巧,下面的程序中唯一能算作技巧的是更换node时有时并不需要切断其与左右节点和父节点的链接,只需要更换val值就可以了。

    Solution 1 ()

    class Solution {
    public:
        TreeNode* removeNode(TreeNode* root, int value) {
            if (root == NULL)
                return NULL;
            TreeNode * head = new TreeNode();
            head->left = root;
            TreeNode * tmp = root, *father = head;
    
            while (tmp != NULL) {
                if (tmp->val == value)
                    break;
                father = tmp;
                if (tmp->val > value)
                    tmp = tmp->left;
                else
                    tmp = tmp->right;
            }
            if (tmp == NULL)
                return head->left;
    
            if (tmp->right == NULL) {
                if (father->left == tmp)
                    father->left = tmp->left;
                else
                    father->right = tmp->left;
            } else 
            if (tmp->right->left == NULL) {
                if (father->left == tmp)
                    father->left = tmp->right;
                else
                    father->right = tmp->right;
    
                tmp->right->left = tmp->left;
                
            } else {
                father = tmp->right;
                TreeNode * cur = tmp->right->left;
                while (cur->left != NULL) {
                    father = cur;
                    cur = cur->left;
                }
                tmp->val = cur->val;
                father->left = cur->right;
            }
            return head->left;
        }
    };

      用右子树最小值替代 

    Solution 2  ()

    class Solution {
    public:
        TreeNode* removeNode(TreeNode* root, int value) {
            if (root == nullptr) {
                return nullptr;
            }
            if (root->val > value) {
                root->left = removeNode(root->left, value);
            } else if (root->val < value) {
                root->right = removeNode(root->right, value);
            } else {
                // leaf node
                if (root->left == nullptr && root->right == nullptr) {
                    root = nullptr;
                } else if (root->left == nullptr) {
                    root = root->right;
                } else if (root->right == nullptr) {
                    root = root->left;
                } else {
                    TreeNode* tmp = findMin(root->right);
                    root->val = tmp->val;
                    root->right = removeNode(root->right, tmp->val);
                }
            }
            
            return root;
        }
        
        TreeNode* findMin(TreeNode* root) {
            while (root->left != nullptr) {
                root = root->left;
            }
            return root;
        }    
    };

      用左子树最大值替代

    Solution 3 ()

    class Solution {
    public:
        TreeNode* removeNode(TreeNode* root, int value) {
            if (root == nullptr) {
                return nullptr;
            }
            if (root->val > value) {
                root->left = removeNode(root->left, value);
            } else if (root->val < value) {
                root->right = removeNode(root->right, value);
            } else {
                // leaf node
                if (root->left == nullptr && root->right == nullptr) {
                    root = nullptr;
                // only one child
                } else if (root->left == nullptr) {
                    root = root->right;
                } else if (root->right == nullptr) {
                    root = root->left;
                // two child
                } else {
                    TreeNode* tmp = findMax(root->left);
                    root->val = tmp->val;
                    root->left = removeNode(root->left, tmp->val);
                }
            }
            
            return root;
        }
        
        TreeNode* findMax(TreeNode* root) {
            while (root->right != nullptr) {
                root = root->right;
            }
            return root;
        }    
    };
  • 相关阅读:
    TSQL--NESTED LOOPS JOIN
    TSQL--SORT MERGE JOIN
    TSQL--HASH JOIN
    Backup--备份相关的信息查看及小技巧
    java后台json如何传递到jsp中解析
    大数据资源
    oracle sql生成日历表
    easyUI中onLoadSuccess方法获取多个对象
    征信用户数据分析
    JS优秀资源站点收集
  • 原文地址:https://www.cnblogs.com/Atanisi/p/6837878.html
Copyright © 2011-2022 走看看