【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 ...
随机推荐
- [RN] React Native Fetch请求设置超时
一.实现思路 根据Promise.race的特性,我们在Promise.race里面添加两个任务,一个是正常的网络请求任务A,另外一个便是网络延时任务B,网络延时可以利用setTimeout方法实现. ...
- ps制作马赛克图片
- bzoj1176: [Balkan2007]Mokia cdq
链接 bzoj 思路 cdq入门题,拆成4个矩阵,然后cdq. 代码 /************************************************************** P ...
- 第03组 Alpha冲刺
队名:不等式方程组 组长博客 作业博客 团队项目进度 组员一:张逸杰(组长) 过去两天完成的任务: 文字/口头描述: 制定了初步的项目计划,并开始学习一些推荐.搜索类算法 GitHub签入纪录: 暂无 ...
- linux netstat 命令简介
常用选项: -r, --route 显示路由表 -i, --interfaces 显示接口信息表-s, --statistics 显示网络协议汇总信息 -n, --numeric 不解析域名-p, - ...
- Spring事务经典案例-银行转账
1.entity实体类 2.dao层 3.dao实现类 4.service层 5.serviceimpl层 6.大配置.xml <?xml version="1.0" enc ...
- linux jar/war包 后台运行
1. 基础版,当前ssh窗口锁定,按CTRL+C打断程序运行:或关闭窗口,程序退出 java -jar flowable-modeler.war 2. 改进版,当前ssh窗口不锁定,窗口关闭时,程序终 ...
- 线程休眠只会用Thread.sleep?那你就弱爆了!
线程休眠是 Java 开发经常会用到的一个手段,就是让当前线程睡一会儿,睡醒之后再继续运行. 咱大多数程序员,多线程虽然学得不好,但线程休眠,无人不知,无人不晓,也都会用,不就是用 Thread.sl ...
- 为什么需要cookie和session
为什么需要cookie和session 在Web发展史中,我们知道浏览器与服务器间采用的是 http协议,而这种协议是无状态的,所以这就导致了服务器无法知道是谁在浏览网页,但很明显,一些网页需要知 ...
- 系统性能工具篇(sar)
转自:系统性能工具篇(sar) 1. 介绍 内容很多 是sysstat软件包的一部分 自动运行:/etc/crontab/sysstat $ cat /etc/cron.d/sysstat # The ...