【leetcode】501. Find Mode in Binary Search Tree
class Solution {
public:
vector<int> modes;
int maxCnt = 0;
int curCnt = 0;
int curNum = 0;
vector<int> findMode(TreeNode* root) {
if (!root) {
return modes;
}
curNum = root->val;
inOrder(root);
return modes;
}
void inOrder(TreeNode* root) {
if (!root) {
return;
}
inOrder(root->left);
if (root->val == curNum) {
curCnt++;
} else {
curCnt = 1;
curNum = root->val;
}
if (curCnt > maxCnt) {
// clear all number that have less occurred times
modes = {};
modes.push_back(curNum);
maxCnt = curCnt;
} else if (curCnt == maxCnt) {
modes.push_back(curNum);
}
inOrder(root->right);
}
};
【leetcode】501. Find Mode in Binary Search Tree的更多相关文章
- 【LeetCode】501. Find Mode in Binary Search Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】701. Insert into a Binary Search Tree 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】Convert Sorted List to Binary Search Tree
Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in as ...
- 【leetcode】Convert Sorted Array to Binary Search Tree
Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...
- 【leetcode】701. 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, in ...
- 【leetcode】Convert Sorted Array to Binary Search Tree (easy)
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 有序 ...
- 【leetcode】Convert Sorted List to Binary Search Tree (middle)
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- 【题解】【BST】【Leetcode】Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.思路: ...
- 【PAT】1043 Is It a Binary Search Tree(25 分)
1043 Is It a Binary Search Tree(25 分) A Binary Search Tree (BST) is recursively defined as a binary ...
随机推荐
- Vue绑定事件,双向数据绑定,只是循环没那么简单
v-on对象处理 <p @mouseover = "doTish" @mouseout = "doThat"> 对象形式 </p> &l ...
- mac 以多tab的方式打开
让mac 始终以多tab的方式打开程序 背景: 开发中始终需要多个 项目 的方式进行处理, 但一直使用 `command + `` 的方式总是比较烦人, 有没有一个比较好的视图方式呢; 即我想在一个窗 ...
- discuz插件开发
首先请修改global里的配载文件$_config['plugindeveloper'] = 2; 然后应用中心,点击设计插件 模块选择管理中心即可在应用里面显示链接 开发资料参考:http://fa ...
- 第10组 团队Git现场编程实战
组员职责分工 姓名 分工 童景霖 博客 朱晓倩 制作UI 万本琳 制作UI 唐怡 制作UI 陈心怡 制作UI 黄永福 测评福州最受欢迎的商圈.后期代码修改和完善 郑志强 测评各个价位的前五美食餐厅代码 ...
- Nginx目录文件列表显示
项目中使用了tomcat,Nginx,测试阶段,生产阶段经常会有些bug需要调查.需要有些日志管理工具,在没有ELK的情况下,可以通过配置nginx来实现基本的日常查看.不需要登录到Linux服务器上 ...
- 为什么 Redis 单线程能支撑高并发?
阅读本文大概需要 4 分钟. 作者:Draveness 最近在看 UNIX 网络编程并研究了一下 Redis 的实现,感觉 Redis 的源代码十分适合阅读和分析,其中 I/O 多路复用(mutipl ...
- 【Beta】Scrum meeting 4
目录 写在前面 进度情况 任务进度表 Beta-1阶段燃尽图 遇到的困难 照片 commit记录截图 小程序前端仓库 技术博客 写在前面 例会时间:5.8 22:30-23:00 例会地点:微信群语音 ...
- Eclipse安装代码反编译插件Enhanced Class Decompiler
在开发过程中,如果想查看引入资源的源代码,可以借助eclipse的插件Enhanced Class Decompiler轻松实现,下面我来讲解一下如何安装使用这个插件. 1.打开Eclipse菜单-& ...
- HTML a标签链接 设置点击下载文件
通常情况下,为文件添加链接后,用户可以通过点击链接,直接将文件下载到本地,如下载 excel 表格等 <a href="/user/test/xxxx.excel">点 ...
- Spring-boot +Shiro 导致事务无效
今天在开发过程中,遇到一个情况,就是事务事务,同项目的别的service都在事务中,可以就是有一个事务失效. 排除了各种情况 1.检查数据库的引擎是否是innoDB 2.方法是否为public 3.这 ...