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 ...
随机推荐
- 存储过程重置SEQUENCE值从新开始。
CREATE OR REPLACE PROCEDURE RESET_SEQUENCE( v_SeqName IN VARCHAR2, v_sqlcode OUT NUMBER, v_sqlerrm O ...
- JSP第五篇【JSTL的介绍、core标签库、fn方法库、fmt标签库】
什么是JSTL JSTL全称为 JSP Standard Tag Library 即JSP标准标签库. JSTL作为最基本的标签库,提供了一系列的JSP标签,实现了基本的功能:集合的遍历.数据的输出. ...
- 短视频图像处理 OpenGL ES 实践
2017年,短视频正以其丰富的内容表现力和时间碎片化的特点,快速崛起,而短视频最具可玩性之处就在支持人脸识别的动态贴图和各种不同效果的美颜.滤镜等.那短视频动态贴纸.滤镜.美颜等功能究竟是如何实现的呢 ...
- 适用初学者:vue2.0构建单页应用最佳实战
参考:https://segmentfault.com/a/1190000007630677 自己gitHub项目地址:https://github.com/shixiaoyanyan/vue-wor ...
- jQuery基礎知識
jQuery基礎知識 $(function(){}) //jQuery先執行一遍再執行其他函數 $(document).ready(fn) //文檔加載完後觸發 1. 刪除$:jQuery.noCon ...
- C++中const几中用法
转载自:http://www.cnblogs.com/lichkingct/archive/2009/04/21/1440848.html 1. const修饰普通变量和指针 const修饰变量,一般 ...
- mxnet的训练过程——从python到C++
mxnet的训练过程--从python到C++ mxnet(github-mxnet)的python接口相当完善,我们可以完全不看C++的代码就能直接训练模型,如果我们要学习它的C++的代码,从pyt ...
- Milk Patterns poj3261(后缀数组)
Milk Patterns Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 9274 Accepted: 4173 Cas ...
- php追加编译GD库
一.准备工作. 安裝 GD 前需要安裝 jpegsrc.v7.tar.gz, libpng-1.6.17.tar.gz, zlib-1.2.8.tar.gz, freetype-2.5.5.tar.g ...
- cors解决ajax请求跨域问题
Access-Control-Allow-Origin: * 适用tomcat部署的项目 在web.xml里添加以下内容 <filter> <filter-name>CorsF ...