当前位置:网站首页>LeetCode - 508. Sum of subtree elements with the most occurrences (traversal of binary tree)
LeetCode - 508. Sum of subtree elements with the most occurrences (traversal of binary tree)
2022-07-03 10:06:00 【Cute at the age of three @d】
Preorder traversal of binary numbers
/** * 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;
}
}
边栏推荐
- Development of intelligent charging pile (I): overview of the overall design of the system
- LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
- 单片机现在可谓是铺天盖地,种类繁多,让开发者们应接不暇
- An executable binary file contains more than machine instructions
- 2021-10-27
- Windows下MySQL的安装和删除
- Circular queue related design and implementation reference 1
- Leetcode bit operation
- Swing transformer details-2
- Stm32 NVIC interrupt priority management
猜你喜欢
Dictionary tree prefix tree trie
Leetcode 300 最长上升子序列
LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
Serial communication based on 51 single chip microcomputer
2.Elment Ui 日期选择器 格式化问题
MySQL root user needs sudo login
Leetcode bit operation
我想各位朋友都应该知道学习的基本规律就是:从易到难
Retinaface: single stage dense face localization in the wild
Blue Bridge Cup for migrant workers majoring in electronic information engineering
随机推荐
LeetCode - 933 最近的请求次数
There is no specific definition of embedded system
单片机现在可谓是铺天盖地,种类繁多,让开发者们应接不暇
Liquid crystal display
Serial communication based on 51 single chip microcomputer
LeetCode - 703 数据流中的第 K 大元素(设计 - 优先队列)
01仿B站项目业务架构
When you need to use some functions of STM32, but 51 can't realize them, 32 naturally doesn't need to learn
Stm32f04 clock configuration
Screen display of charging pile design -- led driver ta6932
2020-08-23
Timer and counter of 51 single chip microcomputer
openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
自動裝箱與拆箱了解嗎?原理是什麼?
The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving
Gpiof6, 7, 8 configuration
Yocto Technology Sharing Phase 4: Custom add package support
My 4G smart charging pile gateway design and development related articles
Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
Stm32f407 key interrupt