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 ...
随机推荐
- python 模拟键盘输入
备忘录 import win32api import win32con win32api.keybd_event(17,0,0,0) #ctrl键位码是17 win32api.keybd_event( ...
- MySQL数据库--创建表,查询
MySQL创建表: 表(一)Student (学生表): CREATE TABLE `Student` ( `sno` ) DEFAULT NULL, `sname` ) DEFAULT NULL, ...
- 关于obj文件的理解
编译器先编译.cpp为obj文件,看看文件内有没有冲突,然后再进行链接,链接头文件引入的lib库等等,然后就生成exe文件了,下面这个图说的很好:
- Leetcode513. Find Bottom Left Tree Value找树左下角的值
给定一个二叉树,在树的最后一行找到最左边的值. 示例 1: 输入: 2 / \ 1 3 输出: 1 示例 2: 输入: 1 / \ 2 3 / / \ 4 5 6 / 7 输出: 7 注意: 您可以假 ...
- warning LNK4098: 默认库“MSVCRT”与其他库的使用冲突;请使用 /NODEFAULTLIB:library
解决方法:是所有项目的这个"代码生成"属性设置保持一致. 项目——属性——配置属性——C/C++——代码生成:他有/MT,/MTd,/Md,/MDd四个选项,你必须让所有使用的库都 ...
- Vuex持久化存储之vuex-persist
在引入mapMutations时报错,解决方法: 1:npm install --save-dev babel-plugin-transform-object-rest-spread 2:在packa ...
- js的相关距离
js的相关距离 一.dom对象的距离 ---dom.style.width : 对象本身的内容宽度(这里必须是内联样式中的width,带px)(content) ---dom.style.height ...
- 在Bat批处理中调用Powershell脚本
##如何在BAT中调用powershell,把下面代码另存为bat格式pushd %~dp0powershell.exe -command ^ "& {set-executionp ...
- python使用matplotlib:subplot绘制多个子图
1 问题描述 matploglib 能够绘制出精美的图表, 有些时候, 我们希望把一组图放在一起进行比较, 有没有什么好的方法呢? matplotlib 中提供的 subplot 可以很好的解决这个问 ...
- jmeter断言步骤
在POST /wordpress/wp-login.php请求,也就是名称为submit login form user的请求上点右键, 选择添加 -> 后置处理器 -> CSS/JQue ...