/**
* 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. nginx php 配置

    nginx php 环境的搭建步骤: 1.nginx 配置: server { listen       4446; server_name  localhost; location / { root ...

  2. MSMQ-发送消息到远程专用队列path格式

    在工作组模式下,远程访问专用队列.在网上找到一篇文章,翻译了一下. 最后结论,直接使用多元素格式名方式,利用IP地址直接对单个或多个目标发送消息      MessageQueue rmQ = new ...

  3. 协程、gevent实现异步io、进程、线程、协程对比

    异步io的说白了就是遇到io操作的时候,就停下来去做别的事情.io分网络io和磁盘io,网络io比如说打开一个网站获取数据,下载一首歌等等,磁盘io就是把数据存到一个文件里面,写到磁盘上. 从网站上获 ...

  4. 【转】每天一个linux命令(43):killall命令

    原文网址:http://www.cnblogs.com/peida/archive/2012/12/21/2827366.html Linux系统中的killall命令用于杀死指定名字的进程(kill ...

  5. 【转】每天一个linux命令(15):tail 命令

    原文网址:http://www.cnblogs.com/peida/archive/2012/11/07/2758084.html tail 命令从指定点开始将文件写到标准输出.使用tail命令的-f ...

  6. 教你如何阅读Oracle数据库官方文档

    < Ask Oracle官方原创 > Oracle 官方文档 数量庞大,而且往往没有侧重点,让oracle新手看起来很费力.但是,仍有很多Oracle使用者认为任何oracle学习资料都比 ...

  7. spring考试

  8. dubbo实战

    http://blog.csdn.net/hejingyuan6/article/details/47403299

  9. TFS 2012如何切换用户

    TFS 2012如何切换用户 编写人:左丘文 2018-3-8 春节假期来后,准备干活的时候,才发现TFS账户登入的是另外一个账户.现在想切换为自己的账户时,发现Vs 2012中没找到可以登出的功能, ...

  10. Google.ProtocolBuffers.dll 之.Net应用(一)

    原创文章,转载必需注明出处:http://www.cnblogs.com/wu-jian/ http://www.cnblogs.com/wu-jian/archive/2011/02/22/1961 ...