当前位置:网站首页>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;
}
}
边栏推荐
- Exception handling of arm
- 单片机职业发展:能做下去的都成牛人了,熬不动就辞职或者改行了
- 开学实验里要用到mysql,忘记基本的select语句怎么玩啦?补救来啦~
- 01仿B站项目业务架构
- Happy Dragon Boat Festival—— Zongzi written by canvas~~~~~
- Oracle数据库 SQL语句执行计划、语句跟踪与优化实例
- Notes on C language learning of migrant workers majoring in electronic information engineering
- Swing transformer details-1
- 4G module designed by charging pile obtains signal strength and quality
- 51 MCU tmod and timer configuration
猜你喜欢
STM32 interrupt priority management
单片机学到什么程度能找到工作,这个标准不好量化
Stm32f407 key interrupt
Runtime. getRuntime(). GC () and runtime getRuntime(). The difference between runfinalization()
对于新入行的同学,如果你完全没有接触单片机,建议51单片机入门
The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving
在三线城市、在县城,很难毕业就拿到10K
The third paper of information system project manager in soft examination
51 MCU tmod and timer configuration
嵌入式本来就很坑,相对于互联网来说那个坑多得简直是难走
随机推荐
03 FastJson 解决循环引用
单片机现在可谓是铺天盖地,种类繁多,让开发者们应接不暇
Drive and control program of Dianchuan charging board for charging pile design
Basic knowledge of communication interface
STM32 general timer 1s delay to realize LED flashing
There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way
2. Elment UI date selector formatting problem
Project cost management__ Cost management technology__ Article 8 performance review
Eight working modes of stm32gpio and chip naming rules
新系列单片机还延续了STM32产品家族的低电压和节能两大优势
Project cost management__ Plan value_ Earned value_ Relationship among actual cost and Countermeasures
自動裝箱與拆箱了解嗎?原理是什麼?
Interruption system of 51 single chip microcomputer
Mysql database underlying foundation column
01 business structure of imitation station B project
Project cost management__ Topic of comprehensive calculation
Application of external interrupts
万字手撕七大排序(代码+动图演示)
Code word in NR
How does the memory database give full play to the advantages of memory?