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的更多相关文章

  1. 【LeetCode】501. Find Mode in Binary Search Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 【LeetCode】701. Insert into a Binary Search Tree 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. 【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 ...

  4. 【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 ...

  5. 【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 ...

  6. 【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. 有序 ...

  7. 【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 ...

  8. 【题解】【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.思路: ...

  9. 【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 ...

随机推荐

  1. 8.8poc包问题

    对于8.8的包的问题:zabbix server设备重启后 zabbix server,mariadb,zabbix agent启动不了.是因为在7代的centos中在主机重启后.自动删除了/var/ ...

  2. openjudge1.1

    目录 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.1.10 1.1.1 描述 对于大部分编程语言来说,编写一个能够输出"H ...

  3. 【09NOIP提高组】Hankson 的趣味题(信息学奥赛一本通 1856)(洛谷 1072)

    题目描述 Hanks 博士是BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫Hankson.现在,刚刚放学回家的Hankson 正在思考一个有趣的问题.今天在课堂上,老师讲解了如何求 ...

  4. 运行java程序的时候出现Exception in thread "main" java.lang.UnsupportedClassVersionError:

    1 Exception in thread "main" java.lang.UnsupportedClassVersionError: com/test/inherited/In ...

  5. zabbix TCP 连接数监控

    直接上配置: 1.修改配置 cat userparameter_tcp_connect.conf UserParameter=tcp_connect.established,/opt/app/zabb ...

  6. mysql-创建用户报错ERROR 1396 (HY000): Operation CREATE USER failed for 'root'@'localhost'

    创建用户: create user ‘test’@’%’ identified by ‘test’; 显示ERROR 1396 (HY000): Operation CREATE USER faile ...

  7. PHP环境搭建-Windows系统下PHP环境搭建

    1.PHP环境搭建的前提是 Apache HTTP Server (Apache 服务器)已经安装部署成功,并可以正常访问到服务器的主页面.Apache HTTP Server 的安装部署已经在上一篇 ...

  8. 自动以读写方式挂载ntfs(新)-苹果之路

    之前的mac下挂载ntfs磁盘的方法在新版本的macos下失效了:<自动以读写方式挂载ntfs-黑苹果之路>,现提供一个有效的方法,系统版本:白苹果10.14.6,参见<Mac OS ...

  9. linux 下core文件生成、路径、格式设置及调试

    core文件生成及调试1 代码 #include<stdio.h> int main() { int *p = NULL; *p = 0; return 0; } 2 在当前shell执行 ...

  10. node框架那些事儿

    一.简单介绍 express:适合初学者,模版引擎,路由,中间件 koa2:核心中间件 eggjs:企业级应用框架 二.express 服务端框架,提供路由功能,异常处理.路由系统+中间件构成web开 ...