leetcode501二叉搜索树中的众数刷题打卡

简介: leetcode501二叉搜索树中的众数刷题打卡
501. 二叉搜索树中的众数

题目描述

给你一个含重复值的二叉搜索树(BST)的根节点 root ,找出并返回 BST 中的所有 众数(即,出现频率最高的元素)。

如果树中有不止一个众数,可以按 任意顺序 返回。

假定 BST 满足如下定义:

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

题解思路

  • 迭代法
  • 中序遍历二叉树,将节点放入一个vector数组中,三个for循环来处理该vector
  • 第一个for循环将vector放进map中
map<int, int> m;
for(int i = 0; i < v.size(); i++){
    m[v[i]]++;
}
  • 第二个for循环找到频率最大的key
int max = INT_MIN;
for(map<int, int>::iterator it = m.begin(); it != m.end(); it++){
   max = max < it->second ? it->second : max; 
}
  • 第三个for循环根据最大频率值找到与之匹配的key并且放入结果vector
vector<int> result;
for(map<int, int>::iterator it = m.begin(); it != m.end(); it++)
    if(it->second == max) result.push_back(it->first);
  • 完整代码
class Solution {
public:
    vector<int> findMode(TreeNode* root) {
        stack<TreeNode*> s;
        vector<int> v;
        int diff = 1000000;
        s.push(root);
        while(!s.empty()){
            TreeNode* cur = s.top(); s.pop();
            if(cur){
                if(cur->right) s.push(cur->right);
                s.push(cur);
                s.push(nullptr);
                if(cur->left) s.push(cur->left);
            }else{
                cur = s.top(); s.pop();
                v.push_back(cur->val);
            }
        }
        map<int, int> m;
        for(int i = 0; i < v.size(); i++){
            m[v[i]]++;
        }
        int max = INT_MIN;
        for(map<int, int>::iterator it = m.begin(); it != m.end(); it++){
            max = max < it->second ? it->second : max; 
        } 
        vector<int> result;
        for(map<int, int>::iterator it = m.begin(); it != m.end(); it++)
            if(it->second == max) result.push_back(it->first);
        return result;     
    }
};
  • 迭代
  • 中序遍历大框架来进行操作
void inorder(TreeNode* root){
  if(!root) return;
    inorder(root->left);
    // 处理逻辑
    inorder(root->right);
}
  • 由于二叉搜索树的中序遍历是升序的,所以可以用记录前一个节点的方法来处理众数,设置一个窗口变量,某数的频率有多高这个窗口变量就有多大,然后设置一个最大窗口变量来代表结果,下列是其具体处理逻辑
if(postnode){
    if(postnode->val == root->val)
        chuangkou++;
    else
        chuangkou = 1;
}else{
    chuangkou = 1;
}
postnode = root;
if(chuangkou == maxchuangkou){
    result.push_back(root->val);
}
if(chuangkou > maxchuangkou){
    maxchuangkou = chuangkou;
    result.clear();
    result.push_back(root->val);
}
  • 完整代码
class Solution {
public:
    int chuangkou;
    int maxchuangkou;
    vector<int> result;
    TreeNode *postnode;
    void inorder(TreeNode* root){
        if(!root) return;
        inorder(root->left);
        if(postnode){
            if(postnode->val == root->val)
                chuangkou++;
            else
                chuangkou = 1;
        }else{
            chuangkou = 1;
        }
        postnode = root;
        if(chuangkou == maxchuangkou){
            result.push_back(root->val);
        }
        if(chuangkou > maxchuangkou){
            maxchuangkou = chuangkou;
            result.clear();
            result.push_back(root->val);
        }
        inorder(root->right); 
    }
    vector<int> findMode(TreeNode* root) {
          inorder(root);
          return result;
    }
};


相关文章
|
28天前
|
数据采集 负载均衡 安全
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
本文提供了多个多线程编程问题的解决方案,包括设计有限阻塞队列、多线程网页爬虫、红绿灯路口等,每个问题都给出了至少一种实现方法,涵盖了互斥锁、条件变量、信号量等线程同步机制的使用。
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
|
12天前
【LeetCode 45】701.二叉搜索树中的插入操作
【LeetCode 45】701.二叉搜索树中的插入操作
8 1
|
12天前
【LeetCode 44】235.二叉搜索树的最近公共祖先
【LeetCode 44】235.二叉搜索树的最近公共祖先
11 1
|
12天前
【LeetCode 48】108.将有序数组转换为二叉搜索树
【LeetCode 48】108.将有序数组转换为二叉搜索树
27 0
|
12天前
【LeetCode 47】669.修剪二叉搜索树
【LeetCode 47】669.修剪二叉搜索树
7 0
|
12天前
【LeetCode 46】450.删除二叉搜索树的节点
【LeetCode 46】450.删除二叉搜索树的节点
9 0
|
12天前
【LeetCode 42】501.二叉搜索树中的众数
【LeetCode 42】501.二叉搜索树中的众数
7 0
|
12天前
【LeetCode 41】530.二叉搜索树的最小绝对差
【LeetCode 41】530.二叉搜索树的最小绝对差
7 0
|
12天前
【LeetCode 40】98.验证二叉搜索树
【LeetCode 40】98.验证二叉搜索树
8 0
|
12天前
【LeetCode 39】700.二叉搜索树中的搜索
【LeetCode 39】700.二叉搜索树中的搜索
11 0