当前位置: 首页 > news >正文

力扣打卡第23天 二叉搜索树中的众数

501. 二叉搜索树中的众数

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

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

假定 BST 满足如下定义:

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

示例 1:

输入:root = [1,null,2,2]
输出:[2]

示例 2:

输入:root = [0]
输出:[0]

提示:

  • 树中节点的数目在范围 [1, 104] 内
  • -105 <= Node.val <= 105

方法一:当做普通的二叉树处理,用map

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:void get_mp(TreeNode* root,unordered_map<int,int>& mp){if(root==nullptr)return;mp[root->val]++;if(root->left)get_mp(root->left,mp);if(root->right)get_mp(root->right,mp);}bool static cmp(pair<int,int> a,pair<int,int>b){//这里少了static就会报错return a.second>b.second;//seconde不用大括号后面}vector<int> findMode(TreeNode* root) {//肯定要用到map,但是进需要无须map即可,后面排序自己排unordered_map<int,int>mp;get_mp(root,mp);vector<pair<int, int>> vec(mp.begin(), mp.end());//mp不能排序,需要先转移到vec里面sort(vec.begin(),vec.end(),cmp);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);}return res;}
};

方法二:利用二叉搜索树性质

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
private:vector<int>res;TreeNode* pre;int max_count;int count;void get(TreeNode* root){if(root==nullptr)return ;if(root->left)get(root->left);//左//中if(pre==nullptr){count=1;pre=root;}else if(pre->val==root->val){count++;pre=root;}else{count=1;pre=root;}if(count>max_count){max_count=count;res.clear();res.push_back(root->val);}else if(count==max_count){res.push_back(root->val);}if(root->right)get(root->right);//右}
public:vector<int> findMode(TreeNode* root) {//利用二叉搜索树的性质(中序遍历有大小关系)max_count=0;pre=nullptr;count=0;res.clear();get(root);return res;}
};

http://www.dtcms.com/a/272396.html

相关文章:

  • 算法题(171):组合型枚举
  • Shusen Wang推荐系统学习 --召回 矩阵补充 双塔模型
  • 深度探索:实时交互与增强现实翻译技术(第六篇)
  • Win10用camke+gcc编译opencv库报错error: ‘_hypot‘ has not been declared in ‘std‘
  • 什么是 领域偏好学习(DPO)与多目标强化学习(PPO)
  • 在 Ubuntu 22 部署 vLLM + Qwen3 32B 模型
  • EPLAN 电气制图(六):电机正反转副勾主电路绘制
  • STM32第十九天 ESP8266-01S和电脑实现串口通信(2)
  • 代理模式——Java
  • 机器学习14——线性回归
  • 前端项目vue3项目集成eslint@9.x跟prettier
  • android TabLayout 标题栏切换 事件拦截
  • 【前端】jQuery动态加载CSS方法总结
  • 2025Datawhale AI夏令营第一期-(1)用AI预测新增用户
  • 01-RabbitMQ消息队列
  • ResolvableType 解密Java泛型反射
  • day01 - 数组part01
  • 【高等数学】第三章 微分中值定理与导数的应用——第二节 洛必达法则
  • 关闭实时防护
  • Qt Creator控件及其用途详细总结
  • LeetCode经典题解:49、字母异位词分组
  • 游戏开发问题记录
  • 数字孪生技术为UI前端赋能:实现产品设计的快速原型验证
  • 小程序开发平台,自主开发小程序源码系统,多端适配,带完整的部署教程
  • Day57
  • 从零开始搭建深度学习大厦系列-2.卷积神经网络基础(5-9)
  • Redis性能基准测试
  • 影刀 RPA:实时追踪网页变化,第一时间推送通知
  • 知微传感Lkam系列线扫轮廓仪SDK例程篇:设置工作逻辑
  • Ubuntu 20.04 下**安装 FFmpeg 5.1