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 element) in the given BST.
Assume a BST is defined as follows:
- The left subtree of a node contains only nodes with keys less than or equal to the node's key.
- The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
- Both the left and right subtrees must also be binary search trees.
For example:
Given BST [1,null,2,2],
1
\
2
/
2
return [2].
Note: If a tree has more than one mode, you can return them in any order.
Follow up: Could you do that without using any extra space? (Assume that the implicit stack space incurred due to recursion does not count).
Java Solution:
Runtime beats 74.31%
完成日期:07/07/2017
关键词:Tree
关键点:inorder 遍历 (从小到大顺序)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution
{
TreeNode pre = null;
int cnt = 1;
int max_cnt = 0; public int[] findMode(TreeNode root)
{
ArrayList<Integer> res = new ArrayList<>(); inorder(root, res); int[] result = new int[res.size()]; for(int i=0; i<result.length; i++)
result[i] = res.get(i); return result;
} public void inorder(TreeNode node, ArrayList<Integer> res)
{
if(node == null)
return; inorder(node.left, res);
// meaning this node has a previous node, need to compare them to determine cnt
if(pre != null)
{
if(node.val == pre.val) // if this node has same value as pre's
cnt++;
else // if this node has different value as pre's
cnt = 1;
} // once cnt is greater max_cnt, meaning find a new mode, need to clear res;
if(cnt > max_cnt)
{
max_cnt = cnt;
res.clear();
res.add(node.val);
}
else if(cnt == max_cnt) // cnt == max_cnt, meaning find a new mode that equal to pre mode.
res.add(node.val); if(pre == null) // for first most left leaf node, its pre is null, set the first pre node
{
pre = new TreeNode(node.val);
}
else // if pre is not null, update this node's val each time
pre.val = node.val; inorder(node.right, res);
}
}
参考资料:
http://www.cnblogs.com/grandyang/p/6436150.html
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 501. Find Mode in Binary Search Tree (找到二叉搜索树的众数)的更多相关文章
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
		Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ... 
- [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树
		4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ... 
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
		Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ... 
- [LeetCode] Recover Binary Search Tree 复原二叉搜索树
		Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ... 
- [LeetCode] Validate Binary Search Tree 验证二叉搜索树
		Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ... 
- LeetCode 235. Lowest Common Ancestor of a Binary Search Tree (二叉搜索树最近的共同祖先)
		Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ... 
- [LeetCode] 255. Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
		Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ... 
- [LeetCode] 99. Recover Binary Search Tree 复原二叉搜索树
		Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ... 
- [LeetCode] 98. Validate Binary Search Tree 验证二叉搜索树
		Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ... 
- [leetcode]98. Validate Binary Search Tree验证二叉搜索树
		Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ... 
随机推荐
- 在centos6,7 上编译安装内核
			小编以前写过一篇软件的源码编译安装,今天小编再给大家带来一篇内核的编译安装. 今天,就以centos7 编译安装最新版本4.13.2 内核为例,给大家详解.编译安装之前,检查一下自己的磁盘空间 ... 
- Hibernate第十篇【Hibernate查询详解、分页查询】
			前言 在Hibernate的第二篇中只是简单地说了Hibernate的几种查询方式-.到目前为止,我们都是使用一些简单的主键查询阿-使用HQL查询所有的数据-.本博文主要讲解Hibernate的查询操 ... 
- c#中的格式输出
			Reference:http://blog.csdn.net/fightfaith/article/details/48137235 
- C# 下搭建最新版OpenCV(Emgu CV)开发环境
			既然是"最新版" 首先当然是去sf找安装包: https://sourceforge.net/projects/emgucv/files/emgucv/ 或着去github主页上c ... 
- mongodb 在windows下面进行副本建创建
			一:主从复制 1: 首先看看模型图 2: 从上面的图形中我们可以分析出这种架构有如下的好处: <1> 数据备份. <2> 数据恢复. <3> 读写分离. 3: ... 
- geotrellis使用(三十四)矢量瓦片技术研究——矢栅一体化
			前言 本文所涉及技术与Geotrellis并无太大关系,仅是矢量瓦片前端渲染和加载技术,但是其实我这是在为Geotrellis的矢量瓦片做铺垫.很多人可能会说,Geotrellis为什么要搞矢量瓦片, ... 
- shell脚本获取文件中key/value的小方法
			方法有N种,awk.sad.grep.cut... 以上几种方式不写了,就写两个不太常用到的. 废话少说,直接上代码: cat a.txt aa.gif=aaaa.gif bb.gif=bbbb.gi ... 
- Spring定时器的使用详解
			写个最简单的demo吧,反正睡前没什么事儿,来祸害一下园子~~虽然我菜,但是我不会承认啊,哈哈哈 明天详细补充点儿吧,很晚了,不睡觉的程序员不是好程序员,我总能给自己找借口~~~ //spring开启 ... 
- window、linux系统与linux服务器之间使用svn同步及自动部署代码的方法
			摘要: 在家用PC,在公司用办公电脑对一个项目的代码进行修改时,会遇到代码同步的问题.本文讲解了代码同步及自动部署的解决办法. 实现方法: 1.首先在linux服务器上和linux上安装svn(sud ... 
- Thread.Join 和 Task.Wait 方法
			这两个方法 可以说是类似的功能,都是对当前任务进行等待阻塞,执行完毕后再进行后续处理 talk is cheap, show you code,下面一个是异步执行,一个是加了阻塞,可以对比不同执行结果 ... 
