(树_19)求BST中的众数

 

501. 二叉搜索树中的众数

给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素)。

假定 BST 有如下定义:

  • 结点左子树中所含结点的值小于等于当前结点的值
  • 结点右子树中所含结点的值大于等于当前结点的值
  • 左子树和右子树都是二叉搜索树

例如:
给定 BST [1,null,2,2],

   1
    \
     2
    /
   2

返回[2].

 解题思路:

参考代码随想录

三种方式:

第一种map+vector+中序可以求所有二叉树的众数

第二种递归,用pre\cur + 中序方式来处理,这里中间节点的处理有节点频率count的取值,最大频率maxCount的更新时机、结果集res的清空时机是重点

第三种是迭代方式,完全模拟递归,中间节点的处理过程跟第二种递归一模一样

```cpp []
/**可通求 二叉树众数
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    unordered_map<int, int> mp; //存放<节点值,频率>
    void searchBST(TreeNode* node){//遍历BST记录节点值对应频率
        if(node == nullptr) return;
        searchBST(node->left); //左
        mp[node->val]++; //中
        searchBST(node->right); //右
    }
    bool static cmp(const pair<int, int> &a, const pair<int, int> &b){
        return a.second > b.second; //按频率排序
    }


    vector<int> findMode(TreeNode* root) {
        if(root == nullptr) return {};

        searchBST(root); //递归将节点对应频率放进mp
        vector<pair<int, int> > vec(mp.begin(), mp.end()); //mp放到vec
        sort(vec.begin(), vec.end(), cmp); //vec排序:由大到小
        
        vector<int> res;
        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;
    }
};
```
```cpp []
/**递归,用pre\cur方式,注意中间节点处理
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int maxCount; //存放最大频率
    int count;     //为每个节点值 计数
    vector<int> res;
    TreeNode *pre = nullptr; //存放上一个节点

    void searchBST(TreeNode *cur){ //中序
        if(cur == nullptr) return;
        searchBST(cur->left); //左

        //计数方式
        if(pre == nullptr) count = 1; //中
        else if(pre != nullptr && pre->val == cur->val) count++;
        else count = 1;

        //根据count、 maxCount更新最大频率
        if(count == maxCount) res.push_back(cur->val);
        if(count > maxCount){
            maxCount = count;
            res.clear();
            res.push_back(cur->val);
        }
        pre = cur; //保存当前节点,到下一次运行到这就是上一个节点了

        searchBST(cur->right); //右
    }


    vector<int> findMode(TreeNode* root) {
        if(root == nullptr) return {};
        maxCount = 0;
        count = 0;
        res.clear();
        searchBST(root);
        return res;
    }
};
```
```cpp []
/**迭代:模拟递归,中间处理方式跟递归一模一样
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> findMode(TreeNode* root) {
        if(root == nullptr) return {};
        int maxCount = 0; //存放最大频率
        int count = 0;     //为每个节点值 计数
        vector<int> res;
        TreeNode *pre = nullptr; //存放上一个节点
        TreeNode *cur = root;
        stack<TreeNode*> st;
        while(cur != nullptr || !st.empty()){
            if(cur != nullptr){//左
                st.push(cur);
                cur = cur->left;
            }else{
                cur = st.top();
                st.pop();

                //计数方式
                if(pre == nullptr) count = 1; //中
                else if(pre != nullptr && pre->val == cur->val) count++;
                else count = 1;

                //根据count、 maxCount更新最大频率
                if(count == maxCount) res.push_back(cur->val);
                if(count > maxCount){
                    maxCount = count;
                    res.clear();
                    res.push_back(cur->val);
                }
                pre = cur; //保存当前节点,到下一次运行到这就是上一个节点了

                cur = cur->right; //右
            }

        }
        return res;
    }
};
```

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Jasscical

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

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

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

打赏作者

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

抵扣说明:

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

余额充值