当前位置:网站首页>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;
}
}
边栏推荐
- Qcombox style settings
- I think all friends should know that the basic law of learning is: from easy to difficult
- [keil5 debugging] warning:enumerated type mixed with other type
- Gpiof6, 7, 8 configuration
- 要選擇那種語言為單片機編寫程序呢
- LeetCode - 919. 完全二叉树插入器 (数组)
- 使用密钥对的形式连接阿里云服务器
- Wireshark use
- The 4G module designed by the charging pile obtains NTP time through mqtt based on 4G network
- yocto 技術分享第四期:自定義增加軟件包支持
猜你喜欢
Opencv Harris corner detection
QT self drawing button with bubbles
JS foundation - prototype prototype chain and macro task / micro task / event mechanism
STM32 interrupt switch
Development of intelligent charging pile (I): overview of the overall design of the system
LeetCode - 706 设计哈希映射(设计) *
Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
The underlying principle of vector
LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
Cases of OpenCV image enhancement
随机推荐
Opencv feature extraction - hog
LeetCode - 673. 最长递增子序列的个数
03 FastJson 解决循环引用
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
对于新入行的同学,如果你完全没有接触单片机,建议51单片机入门
2020-08-23
Seven sorting of ten thousand words by hand (code + dynamic diagram demonstration)
Tensorflow built-in evaluation
The 4G module designed by the charging pile obtains NTP time through mqtt based on 4G network
Gpiof6, 7, 8 configuration
pycharm 无法引入自定义包
My notes on the development of intelligent charging pile (III): overview of the overall design of the system software
Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
(2) New methods in the interface
In third tier cities and counties, it is difficult to get 10K after graduation
yocto 技术分享第四期:自定义增加软件包支持
Embedded systems are inherently flawed. Compared with the Internet, there are so many holes that it is simply difficult to walk away from
Opencv histogram equalization
STM32 general timer output PWM control steering gear
Basic knowledge of communication interface