Leetcode 二叉树 501 236 235 701 450

二叉搜索树操作:查找模式、最近公共祖先与插入删除
文章讨论了在二叉搜索树中进行的操作,包括查找出现次数最多的节点(findMode)、找到两个节点的最近公共祖先(LowestCommonAncestor)以及插入和删除节点(insertIntoBST,deleteNode)。两种方法分别使用递归和迭代,以及unordered_map辅助数据结构。

501. Find Mode in Binary Search Tree

class Solution {
private:
    vector<int> num;
    void traversal(TreeNode* root){
        if(root == NULL) return;
        traversal(root->left);
        num.push_back(root->val);
        traversal(root->right);
    }
public:
    vector<int> findMode(TreeNode* root) {
        traversal(root);
        if(num.size() == 1) return vector<int>{num[0]};
        vector<int> res;
        res.push_back(num[0]);
        int maxNum = 1;
        int count = 1;
        for(int i=1; i<num.size(); i++){
            if(num[i] == num[i-1]) count++;
            else count = 1;

            if(count >= maxNum){
                if(count > maxNum){
                    res.clear();
                }
                maxNum = count;
                res.push_back(num[i]);
            }
        }
        return res;
    }
};

用unordered_map(不是搜索二叉树也可以用)

class Solution {
private:
    unordered_map<int, int> map;
    void traversal(TreeNode* root){
        if(root == NULL) return;
        traversal(root->left);
        map[root->val]++;
        traversal(root->right);
    }
    bool static cmp(const pair<int, int>& a, const pair<int, int> b){
        return a.second > b.second;
    } // 从大到小
public:
    vector<int> findMode(TreeNode* root) {
        traversal(root);
        vector<int> res;
        vector<pair<int, int>> vec(map.begin(), map.end());
        sort(vec.begin(), vec.end(), cmp);
        res.push_back(vec[0].first);
        for(int i=1; i<vec.size(); i++){
            if(vec[i].second == vec[0].second){
                res.push_back(vec[i].first);
            }
        }

        return res;
    }

};

236. Lowest Common Ancestor of a Binary Tree

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(root == NULL) return NULL;
        if(root == p || root == q) return root;

        TreeNode* left = lowestCommonAncestor(root->left, p, q);
        TreeNode* right = lowestCommonAncestor(root->right, p, q);

        if(left != NULL && right != NULL) return root;
        if(left == NULL && right == NULL) return NULL;
        return left == NULL ? right:left;
    }
};

这道题要从最下面开始找,当p,q分别在左右两端的时候证明此时就为公共祖先

 235. Lowest Common Ancestor of a Binary Search Tree

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(root == NULL) return NULL;
        if(root == p || root == q) return root;
        if(p->val < q->val){
            swap(p, q);
        }
        if(root->val < p->val && root->val > q->val) return root;
        else if(root->val > p->val) return lowestCommonAncestor(root->left, p, q);
        else return lowestCommonAncestor(root->right, p, q);
    }
};

因为可以利用bst的特点,所以从上往下找就可以了

701. Insert into a Binary Search Tree

1.递归

class Solution {
public:
    TreeNode* insertIntoBST(TreeNode* root, int val) {
        if(root == NULL){
            TreeNode* newNode = new TreeNode(val);
            return newNode;
        }

        if(root->val > val) root->left = insertIntoBST(root->left, val);
        if(root->val < val) root->right = insertIntoBST(root->right, val);

        return root;
        
    }
};

2.迭代

class Solution {
public:
    TreeNode* insertIntoBST(TreeNode* root, int val) {
        if(root == NULL){
            TreeNode* newNode = new TreeNode(val);
            return newNode;
        }

        TreeNode* cur = root;
        TreeNode* parent = root;
        while(cur != NULL){
            parent = cur;
            if(cur->val > val) cur = cur->left;
            else cur = cur->right;
        }
        TreeNode* node = new TreeNode(val);
        if(parent->val > val) parent->left = node;
        else parent->right = node;
        return root;
        
    }
};

450. Delete Node in a BST

class Solution {
private:
    TreeNode* getMin(TreeNode* root){
        while(root->left != NULL) root = root->left;
        return root;
    }
public:
    TreeNode* deleteNode(TreeNode* root, int key) {
        if(root == NULL) return root;

        if(root->val > key) root->left = deleteNode(root->left, key);
        if(root->val < key) root->right = deleteNode(root->right, key);
        if(root->val == key){
            if(root->left == NULL) return root->right;
            if(root->right == NULL) return root->left;
            if(root->left != NULL && root->right != NULL){
                TreeNode* node = getMin(root->right);
                root->right = deleteNode(root->right, node->val);
                node->left = root->left;
                node->right = root->right;
                root = node;
            } 
        } 

        return root;
    }
};

遇到删除的节点,一共会有三种情况

1.左右都没有节点,直接删掉

2.有一个节点, 用这个节点取代原来的节点

3.左右都有节点,要用左子树的最大值,或者右子树的最小值替换

注意

1.第三种情况在替换的时候,要记得删除掉原本右子树最小的值

2.因为这里要返回root, 所以root的值不能改变

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值