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. Python 11 提取括号中间的内容

    原文:https://blog.csdn.net/your_answer/article/details/80456550 import re string = 'abe(ac)ad)' p1 = r ...

  2. FPGA综合的约束

    近日发现,有些逻辑电路的综合时间约束和布局布线约束相差太大时,难以布通.此时,应该选择尽量接近的时钟约束.

  3. AttributeError: module ‘select’ has no attribute 'epoll’

    场景:mac 下导入的 ‘select’ 包 import select,然后在 主函数 中创建的 epoll 对象 epl = select.epoll(),运行报错如下 Traceback (mo ...

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

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

  5. c# 画正态分布图

    /// <summary> /// 提供正态分布的数据和图片 /// </summary> public class StandardDistribution { /// &l ...

  6. windows安装IIS不成功的原因

    一.背景 之前做过一段时间的实施,因此总结一下IIS安装不成功会有哪些原因导致的,希望给踩坑的人提供思路和帮助. 二.分析原因 1.系统问题,比如Windows家庭版本(独白:我之前花了一天的时间安装 ...

  7. 关于用maven创建的springboot工程打包jar后找不到配置文件的问题

    你的resources文件夹的名称写错了!!! 如果不在pom中配置build的资源路径,那么你的资源文件名必须默认是“resources”! 解决办法有两个: 1.修改文件夹名称 2.添加pom的b ...

  8. mac jq for json format

    mac jq #1.安装 brew install jq #2.创建文件 echo '{"name": "Ruby"}' > ./test.json #3 ...

  9. Metasploit使用内网跳板, 扫描局域网主机

    最近,拿到一台内网机器, 苦于无法使用nmap扫描改主机的内网, 所以才有此文 在跳板机子获取一定权限后,需要积极的向内网主机权限发展,获取指定的目标信息,探查系统漏洞,借助msf已经得到的meter ...

  10. web3 编译部署调用合约

    //导入solc 编译器 let solc = require('solc') let fs = require('fs') //读取合约 let sourceCode = fs.readFileSy ...