当前位置:网站首页>LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
2022-07-03 09:20:00 【三岁就很萌@D】
二叉数的先序遍历
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */
class Solution {
private int maxfreq;
private Map<Integer,Integer> count;
private List<Integer> ans;
public int sumTree(TreeNode root){
int sum = 0;
if(root == null)
return 0;
else{
sum += root.val;
sum += sumTree(root.left);
sum += sumTree(root.right);
count.put(sum,count.getOrDefault(sum,0)+1);
if(count.getOrDefault(sum,0).compareTo(maxfreq)>0)
{
maxfreq = count.getOrDefault(sum,0);
ans = new ArrayList<>();
ans.add(sum);
}
else if(count.getOrDefault(sum,0).compareTo(maxfreq) == 0){
ans.add(sum);
}
return sum;
}
}
public int[] findFrequentTreeSum(TreeNode root) {
maxfreq = 0;
count = new HashMap<>();
ans = new ArrayList<>();
sumTree(root);
int[] ansArray = new int[ans.size()];
int index = 0;
for(int i =0 ; i< ansArray.length;i++)
ansArray[index++] = ans.get(i);
return ansArray;
}
}
边栏推荐
- [keil5 debugging] warning:enumerated type mixed with other type
- Fundamentals of Electronic Technology (III)__ Logic gate symbols in Chapter 5
- It is difficult to quantify the extent to which a single-chip computer can find a job
- Project cost management__ Cost management technology__ Article 7 completion performance index (tcpi)
- (1) 什么是Lambda表达式
- [Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation
- Application of 51 single chip microcomputer timer
- For new students, if you have no contact with single-chip microcomputer, it is recommended to get started with 51 single-chip microcomputer
- 03 fastjason solves circular references
- 自動裝箱與拆箱了解嗎?原理是什麼?
猜你喜欢
It is difficult to quantify the extent to which a single-chip computer can find a job
单片机职业发展:能做下去的都成牛人了,熬不动就辞职或者改行了
Comment la base de données mémoire joue - t - elle l'avantage de la mémoire?
03 FastJson 解决循环引用
单片机学到什么程度能找到工作,这个标准不好量化
Uniapp realizes global sharing of wechat applet and custom sharing button style
NR PUCCH format0 sequence generation and detection mechanism
Project cost management__ Topic of comprehensive calculation
新系列单片机还延续了STM32产品家族的低电压和节能两大优势
Embedded systems are inherently flawed. Compared with the Internet, there are so many holes that it is simply difficult to walk away from
随机推荐
没有多少人能够最终把自己的兴趣带到大学毕业上
yocto 技术分享第四期:自定义增加软件包支持
Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
手机都算是单片机的一种,只不过它用的硬件不是51的芯片
Basic knowledge of communication interface
Code word in NR
Application of external interrupts
01 business structure of imitation station B project
端午节快乐!—— canvas写的粽子~~~~~
Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program
El table X-axis direction (horizontal) scroll bar slides to the right by default
Timer and counter of 51 single chip microcomputer
I think all friends should know that the basic law of learning is: from easy to difficult
Fundamentals of Electronic Technology (III)__ Chapter 6 combinational logic circuit
4G module initialization of charge point design
Assignment to '*' form incompatible pointer type 'linkstack' {aka '*'} problem solving
QT qcombobox QSS style settings
Fundamentals of Electronic Technology (III)_ Integrated operational amplifier and its application__ Basic arithmetic circuit
There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way
My openwrt learning notes (V): choice of openwrt development hardware platform - mt7688