428-二叉树(501.二叉搜索树中的众数、701.二叉搜索树中的插入操作、450.删除二叉搜索树中的节点、669. 修剪二叉搜索树)

501. 二叉搜索树中的众数

在这里插入图片描述

class Solution {
public:
    void search_bst(TreeNode* cur, unordered_map<int, int>& mp)
    {
        if (cur == nullptr)  return;
        mp[cur->val]++;
        search_bst(cur->left, mp);
        search_bst(cur->right, mp);
    }
public:
    vector<int> findMode(TreeNode* root) {
        unordered_map<int, int> mp;
        vector<int> res;
        if (root == nullptr) return res;
        search_bst(root, mp);

        vector<pair<int, int>> vec(mp.begin(), mp.end());
        sort(vec.begin(), vec.end(),
            [](const pair<int, int>& x, const pair<int, int>& y) -> bool
            {
                return x.second > y.second;
            });

        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);
            else break;
        }
        return res;
    }
};

在这里插入图片描述

701. 二叉搜索树中的插入操作

在这里插入图片描述

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

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

        return root;
    }
};

在这里插入图片描述

450.删除二叉搜索树中的节点

在这里插入图片描述

class Solution {
public:
    TreeNode* deleteNode(TreeNode* root, int key) {
        if (root == nullptr) return root;
        if (root->val == key)
        {
            if (root->left == nullptr && root->right == nullptr)
            {
                delete root;
                return nullptr;
            }
            else if (root->left == nullptr)
            {
                TreeNode* retNode = root->right;
                delete root;
                return retNode;
            }
            else if (root->right == nullptr)
            {
                TreeNode* retNode = root->left;
                delete root;
                return retNode;
            }
            else
            {
                TreeNode* cur = root->right;
                while (cur->left != nullptr)
                {
                    cur = cur->left;
                }
                cur->left = root->left;
                TreeNode* tmp = root;
                root = root->right;
                delete tmp;
                return root;
            }
        }
        if (root->val > key) root->left = deleteNode(root->left, key);
        if (root->val < key)  root->right = deleteNode(root->right, key);

        return root;
    }
};

在这里插入图片描述

669. 修剪二叉搜索树

在这里插入图片描述

class Solution {
public:
    TreeNode* trimBST(TreeNode* root, int low, int high) {
        if (root == nullptr) return nullptr;
        if (root->val < low)
        {
            TreeNode* right = trimBST(root->right, low, high);
            return right;
        }
        if (root->val > high)
        {
            TreeNode* left = trimBST(root->left, low, high);
            return left;
        }
        root->left = trimBST(root->left, low, high);
        root->right = trimBST(root->right, low, high);

        return root;
    }
};

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liufeng2023

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值