查找二叉树中出现次数最多的数 Find Mode in Binary Search Tree
为什么80%的码农都做不了架构师?>>>
问题:
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).
解决:
① 利用一个哈希表来记录数字和其出现次数之间的映射,然后维护一个变量max来记录当前最多的次数值,这样在遍历完树之后,根据这个max值就能把对应的元素找出来。使用中序遍历方式递归遍历该树(注:使用任何一种遍历方式都可以 )。时间和空间复杂度都是O(N)。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution { // 16ms
private int max = 0;//因为在两个方法中都使用到了这两个变量,所以将其声明在方法外面。
private Map<Integer,Integer> map = new HashMap<>();
public int[] findMode(TreeNode root) {
if (root == null) return new int[0];
List<Integer> list = new ArrayList<>();
inorder(root);
for (Map.Entry<Integer,Integer> entry : map.entrySet()) {
if(entry.getValue() == max) list.add(entry.getKey());
}
int[] res = new int[list.size()];
for (int i = 0;i < list.size() ;i ++ ) {
res[i] = list.get(i);
}
return res;
}
public void inorder(TreeNode node){
if(node == null) return;
if(node.left != null)inorder(node.left);
map.put(node.val,map.getOrDefault(node.val,0) + 1);
max = Math.max(max,map.get(node.val));
if(node.right != null)inorder(node.right);
}
}
② 中序遍历得到一个递增的序列,使用一个计数器count记录相同的值的个数,max记录最大个数,pre记录前一个节点的值用于判断当前节点与前一个是否相等,从而决定要如何操作count;时间复杂度O(n),空间复杂度O(1)
public class Solution { //5 ms
int pre = Integer.MIN_VALUE;//记录前一个节点数值,用于比较当前节点与之前的节点是否相等
int count = 0;
int max = 0;
public int[] findMode(TreeNode root) {
List<Integer> list = new ArrayList<>();
inorder(root, list);//中序遍历
int[] arr = new int[list.size()];
for(int i = 0;i < arr.length;i ++){
arr[i] = list.get(i);
}
return arr;
}
public void inorder(TreeNode root, List<Integer> list){
if(root != null){
inorder(root.left, list);
if(root.val == pre){
count ++;
}else{
count = 1;
pre = root.val;
}
if(count > max){
max = count;
list.clear();
list.add(root.val);
}else if(count == max){
list.add(root.val);
}
inorder(root.right, list);
}
}
}
转载于:https://my.oschina.net/liyurong/blog/1505273
查找二叉树中出现次数最多的数 Find Mode in Binary Search Tree相关推荐
- C语言简单题-求整数序列中出现次数最多的数
7-2 求整数序列中出现次数最多的数 (15分) 本题要求统计一个整型序列中出现次数最多的整数及其出现次数. 输入格式: 输入在一行中给出序列中整数个数N(0<N≤1000),以及N个整数.数字 ...
- JAVA-求整数序列中出现次数最多的数
R7-3 JAVA-求整数序列中出现次数最多的数 (10 分) 要求统计一个整型序列中出现次数最多的整数及其出现次数. 输入格式: 在一行中给出序列中整数个数N(0<N≤1000),依次给出N个 ...
- 7-55 求整数序列中出现次数最多的数 (15 分)
7-55 求整数序列中出现次数最多的数 (15 分) 本题要求统计一个整型序列中出现次数最多的整数及其出现次数. 输入格式: 输入在一行中给出序列中整数个数N(0<N≤1000),以及N个整数. ...
- python查找数组中出现次数最多的元素
方法1-np.argmax(np.bincount()) 看一个例子 array = [0,1,2,2,3,4,4,4,5,6] print(np.bincount(array)) print(np. ...
- python返回列表中出现次数最多的数
def majorityCnt(classList):'''返回标签列表中最多的标签'''count_dict = {}for label in classList:if label not in c ...
- 【面试被虐】如何只用2GB内存从20亿,40亿,80亿个整数中找到出现次数最多的数?...
这几天小秋去面试了,不过最近小秋学习了不少和位算法相关文章,例如 [面试现场]如何判断一个数是否在40亿个整数中? [算法技巧]位运算装逼指南 对于算法题还是有点信心的,,,,于是,发现了如下对话. ...
- 如何只用2GB内存从20/40/80亿个整数中找到出现次数最多的数
来源:公众号[苦逼的码农] 这几天小秋去面试了,不过最近小秋学习了不少和位算法相关文章,例如: [算法技巧]位运算装逼指南 对于算法题还是有点信心的,,,,于是,发现了如下对话. 20亿级别 面试官: ...
- 【面试被虐】如何只用2GB内存从20亿,40亿,80亿个整数中找到出现次数最多的数?
这几天小秋去面试了,不过最近小秋学习了不少和位算法相关文章,例如 [面试现场]如何判断一个数是否在40亿个整数中? [算法技巧]位运算装逼指南 对于算法题还是有点信心的,,,,于是,发现了如下对话. ...
- 只用2GB的内存找出20亿个整数中找到出现次数最多的数
要求有一个包含20亿个32位整数的文件,从中找到出现次数最多的数. 首先先分析一下,32位int类型的数占4B,20亿个4B 约为 8GB,只用2GB肯定不够.所以我们肯定需要将这20亿个数哈希到不同 ...
最新文章
- 《Nature》发布毫米级软体机器人,可在没有任何物理干预情况下游走于人体
- 云炬Android开发笔记 13购物车,订单,支付功能开发(包含支付宝支付和微信支付)
- java图片的导出,并压缩
- ionic 修改组件默认样式_开源Magpie:组件库详解
- windows查看端口占用 windows端口占用 查找端口占用程序 强制结束端口占用 查看某个端口被占用的解决方法 如何查看Windows下端口占用情况
- Flutter代码锦囊---摇一摇
- Android OpenGL ES 开发教程(24):Depth Buffer
- 创业经验点滴 五(转) 关于淘宝
- 台风怎么看内存颗粒_RGB灯效内存条怎么选,看老胡推荐
- Spring Security的工作原理
- html下边框样式,css border-bottom div css下边框样式设置
- 罗永浩2014 一个理想主义者的创业故事Ⅳ 演讲稿实录
- 毕业设计-电子商务网站(二)
- ubuntu su 密码
- 【java并发探赜索隐】判断标志、死锁、守护进程
- 软件工程——(1)软件与软件工程 思维导图
- java 折扣_Java折扣计算
- iOS及Mac开源项目和学习资料
- HTTP协议实现文件下载
- BZOJ_3238_[Ahoi2013]差异_后缀自动机