/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
Dictionary<int, int> dic = new Dictionary<int, int>(); /// <summary>
/// 中序遍历
/// </summary>
/// <param name="root"></param>
private void InNode(TreeNode root)
{
if (root != null)
{
if (root.left != null)
{
InNode(root.left);
} if (!dic.ContainsKey(root.val))
{
dic.Add(root.val, );
}
else
{
dic[root.val]++;
} if (root.right != null)
{
InNode(root.right);
}
}
} public int[] FindMode(TreeNode root)
{
if (root == null)
{
return new int[];
}
InNode(root);
var modelist = new List<int>();
var list = dic.OrderByDescending(x => x.Value).ToList();
var maxSize = ; foreach (var d in list)
{
if (maxSize <= d.Value)
{
maxSize = d.Value;
modelist.Add(d.Key);
}
else
{
break;
}
}
return modelist.ToArray();
}
}

https://leetcode.com/problems/find-mode-in-binary-search-tree/#/description

leetcode501的更多相关文章

  1. [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 ...

  2. leetcode-501. Find Mode in Binary Search Tree

    Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...

  3. Leetcode501.Find Mode in Binary Search Tree二叉搜索树中的众数

    给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素). 假定 BST 有如下定义: 结点左子树中所含结点的值小于等于当前结点的值 结点右子树中所含结点的值大于等于当 ...

  4. LeetCode501.二叉搜索树中的众数

    题目,本题未做出,还有很多要学习 class Solution { public: vector<int>ans; int base,count,maxCount; void update ...

  5. leetcode_二叉树篇_python

    主要是深度遍历和层序遍历的递归和迭代写法. 另外注意:因为求深度可以从上到下去查 所以需要前序遍历(中左右),而高度只能从下到上去查,所以只能后序遍历(左右中). 所有题目首先考虑root否是空.有的 ...

  6. LeetCode通关:连刷三十九道二叉树,刷疯了!

    分门别类刷算法,坚持,进步! 刷题路线参考:https://github.com/youngyangyang04/leetcode-master 大家好,我是拿输出博客来督促自己刷题的老三,这一节我们 ...

随机推荐

  1. 【vue】vue-cli 脚手架项目简介(一) - package.json

    vue-cli是用来生成 vue项目的命令行工具,它的使用方法是这样的: vue init <template-name> <project-name>第二个参数 templa ...

  2. 枚举大小为k的子集

    这种位操作不大可能分析出来,先看代码再分析. 代码 使用条件:\(k>0\) void solve(int n,int k) { for(int comb = (1 << k) - ...

  3. vim golang 插件

    最好用的vim golang 插件 可自动缩进 git clone git@github.com:aimin/InstallvimGo.git

  4. C语言运算符优先级和ASCII表

    1. C语言运算符优先级及结合性 优先级 运算符 名称或含义 使用形式 结合方向 说明 1 [] 数组下标 数组名[常量表达式] 左到右 -- () 圆括号 (表达式)/函数名(形参表) -- . 成 ...

  5. citus 多租户应用开发(来自官方文档)

      citus 官方文档很不错,资料很全,同时包含一个多租户应用的文档,所以运行下,方便学习 环境准备 使用docker-compose 运行,同时集成了graphql 引擎,很方便 docker-c ...

  6. Sql语句导出数据库表结构及查询表视图储存过程名

    --一句Sql把表结构全部查询出来 SELECT 表名 = Case When A.colorder=1 Then D.name Else '' End, 表说明 = Case When A.colo ...

  7. mime type 类型名字应该用多长的字段?

    在使用 FastAdmin 时有 mimetype 字段使用了 50 长度,有小伙伴反应,不够. 在 Linux 服务器上时 xlsx 文件的 mimetype  是 application/vnd. ...

  8. 【转】每天一个linux命令(38):cal 命令

    原文网址:http://www.cnblogs.com/peida/archive/2012/12/14/2817473.html cal命令可以用来显示公历(阳历)日历.公历是现在国际通用的历法,又 ...

  9. Vuejs项目的Webpack2构建优化

    最近在做的项目因为相对较大(打包有100多个chunk),在build构建的时候速度一直上不去,甚是烦恼.由于用的是vue-cli的webpack2模板,一开始并没有想着要对其进行优化,一直觉得是we ...

  10. centos6 安装GitLab

    环境 Requirements 软件 版本CentOS 6.6Python 2.6Ruby 2.1.5Git 1.7.10+Redis 2.0+MySQL GitLab 7-8-stableGitLa ...