Leetcode501.Find Mode in Binary Search Tree二叉搜索树中的众数
给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素)。
假定 BST 有如下定义:
- 结点左子树中所含结点的值小于等于当前结点的值
- 结点右子树中所含结点的值大于等于当前结点的值
- 左子树和右子树都是二叉搜索树
例如:
给定 BST [1,null,2,2],
1 \ 2 / 2
返回[2].
提示:如果众数超过1个,不需考虑输出顺序
class Solution {
public:
map<int, int> check;
int MAX = 0;
vector<int> findMode(TreeNode* root)
{
Fun(root);
map<int, int>:: iterator itr;
vector<int> res;
for(itr = check.begin(); itr != check.end(); itr++)
{
if(itr ->second == MAX)
{
res.push_back(itr ->first);
}
}
return res;
}
void Fun(TreeNode* root)
{
if(root == NULL)
return ;
check[root ->val]++;
MAX = max(MAX, check[root ->val]);
Fun(root ->left);
Fun(root ->right);
}
};
Leetcode501.Find Mode in Binary Search Tree二叉搜索树中的众数的更多相关文章
- [LeetCode] Insert into a Binary Search Tree 二叉搜索树中插入结点
Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...
- [LeetCode] Search in a Binary Search Tree 二叉搜索树中搜索
Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST ...
- Leetcode700.Search in a Binary Search Tree二叉搜索树中的搜索
给定二叉搜索树(BST)的根节点和一个值. 你需要在BST中找到节点值等于给定值的节点. 返回以该节点为根的子树. 如果节点不存在,则返回 NULL. class Solution { public: ...
- LeetCode Recover Binary Search Tree——二查搜索树中两个节点错误
Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing ...
- Java实现 LeetCode 501 二叉搜索树中的众数
501. 二叉搜索树中的众数 给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素). 假定 BST 有如下定义: 结点左子树中所含结点的值小于等于当前结点的值 结点 ...
- [Swift]LeetCode501. 二叉搜索树中的众数 | Find Mode in Binary Search Tree
Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...
- [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...
- [LeetCode] 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
随机推荐
- day 46 Javascript学习
Javascript学习 JavaScript概述 ECMAScript和JavaScript的关系 1996年11月,JavaScript的创造者--Netscape公司,决定将JavaScri ...
- mysql80版本—yum安装—图文全过程
这是官网的Quick Giude:https://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/ 以下为自己安装的步骤: 第一步:下载.rpm安装包 ...
- Maven入门指南
Maven入门指南 本指南旨在第一次为使用Maven的人员提供参考,但也打算作为一本包含公共用例的独立参考和解决方案的工具书.对于新用户,建议您按顺序浏览该材料.对于更熟悉Maven的用户,本指南致力 ...
- css 超出两行省略号,超出一行省略号
参考:https://www.cnblogs.com/yangguojin/p/10301981.html 超出一行省略: p{ white-space:nowrap; overflow:hidden ...
- 3.Spring框架中的标签与配置文件分离
1.Spring框架中标签的配置 1. id属性和name属性的区别 * id -- Bean起个名字,在约束中采用ID的约束,唯一 * 取值要求:必须以字母开始,可以使用字母.数字.连字符.下划线. ...
- 原生js封装ajax代码
方法一:(类似jQuery的封装方法) 1.ajax函数封装: /* *author: Ivan *date: 2014.06.01 *参数说明: *opts: {'可选参数'} **method: ...
- idea永久使用本地的maven设置
1.要想是永久的,就选择other settings
- html常用标签详解2-图片标签详解
<img /> 1.图片标签的属性 图片标签属于行内块元素,它自身的属性有一下几个,听我娓娓道来: src:图片资源的路径(resourse),可以使绝对路径,也可以是相对路径 绝对路径: ...
- LUOGU P1970 花匠 (Noip 2013)
传送门 解题思路 好多大佬用的dp啊,貌似贪心可以做,每次所选的一定是每个连续递增或递减序列的最后,直接模拟就行了,注意判断一下头和尾相等的情况. #include<iostream> # ...
- PhpStorm中terminal窗口字体修改
在PhpStorm–File–Settings–Tools–Terminal中可以看到terminal调用的系统的cmd.exe程序 因此需要做的就是修改系统的cmd.exe中的字体,如下: CMD命 ...