当前位置:网站首页>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;
}
}
边栏推荐
- Emballage automatique et déballage compris? Quel est le principe?
- Project cost management__ Topic of comprehensive calculation
- 新系列单片机还延续了STM32产品家族的低电压和节能两大优势
- 单片机职业发展:能做下去的都成牛人了,熬不动就辞职或者改行了
- STM32 serial port usart1 routine
- 我想各位朋友都应该知道学习的基本规律就是:从易到难
- Oracle数据库 SQL语句执行计划、语句跟踪与优化实例
- Gif image analysis drawing RGB to YUV table lookup method to reduce CPU occupancy
- 4G module initialization of charge point design
- Embedded systems are inherently flawed. Compared with the Internet, there are so many holes that it is simply difficult to walk away from
猜你喜欢

2021-10-28

I think all friends should know that the basic law of learning is: from easy to difficult

Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip

编程思想比任何都重要,不是比谁多会用几个函数而是比程序的理解

SCM career development: those who can continue to do it have become great people. If they can't endure it, they will resign or change their careers

Happy Dragon Boat Festival—— Zongzi written by canvas~~~~~

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

Stm32f407 key interrupt

An executable binary file contains more than machine instructions

03 FastJson 解决循环引用
随机推荐
Basic knowledge of MySQL database (an introduction to systematization)
Installation and removal of MySQL under Windows
2020-08-23
GPIO port details, Hal library operation keys
一个可执行的二进制文件包含的不仅仅是机器指令
4G module IMEI of charging pile design
03 FastJson 解决循环引用
[keil5 debugging] warning:enumerated type mixed with other type
Oracle数据库 SQL语句执行计划、语句跟踪与优化实例
QT qcombobox QSS style settings
嵌入式系统没有特别明确的定义
Getting started with JMX, MBean, mxbean, mbeanserver
The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving
要選擇那種語言為單片機編寫程序呢
STM32 serial port usart1 routine
byte alignment
Not many people can finally bring their interests to college graduation
STM32 interrupt switch
Assignment to '*' form incompatible pointer type 'linkstack' {aka '*'} problem solving
学习开发没有捷径,也几乎不存在带路会学的快一些的情况