当前位置:网站首页>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;
}
}
边栏推荐
- [Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation
- 嵌入式系统没有特别明确的定义
- STM32 port multiplexing and remapping
- Chromium Embedded Framework (CEF) 介绍
- STM32 interrupt switch
- (2) New methods in the interface
- Liquid crystal display
- Eight working modes of stm32gpio and chip naming rules
- There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way
- (1) What is a lambda expression
猜你喜欢

I didn't think so much when I was in the field of single chip microcomputer. I just wanted to earn money to support myself first

Notes on C language learning of migrant workers majoring in electronic information engineering

There is no specific definition of embedded system

There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way

Interruption system of 51 single chip microcomputer

没有多少人能够最终把自己的兴趣带到大学毕业上

Which language should I choose to program for single chip microcomputer

单片机学到什么程度能找到工作,这个标准不好量化

要選擇那種語言為單片機編寫程序呢

Fundamentals of Electronic Technology (III)__ Chapter 1 resistance of parallel circuit
随机推荐
There is no specific definition of embedded system
干单片机这一行的时候根本没想过这么多,只想着先挣钱养活自己
STM32 general timer 1s delay to realize LED flashing
Education is a pass and ticket. With it, you can step into a higher-level environment
Quelle langue choisir pour programmer un micro - ordinateur à puce unique
2021-10-28
Application of external interrupts
It is difficult to quantify the extent to which a single-chip computer can find a job
2. Elment UI date selector formatting problem
Chromium Embedded Framework (CEF) 介绍
01仿B站项目业务架构
My notes on intelligent charging pile development (II): overview of system hardware circuit design
Interruption system of 51 single chip microcomputer
Windows下MySQL的安装和删除
端午节快乐!—— canvas写的粽子~~~~~
Stm32f04 clock configuration
学历是一张通行证,门票,你有了它,可以踏入更高层次的环境里
[CSDN] C1 training problem analysis_ Part IV_ Advanced web
编程思想比任何都重要,不是比谁多会用几个函数而是比程序的理解
万字手撕七大排序(代码+动图演示)