当前位置:网站首页>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;
}
}
边栏推荐
- Swing transformer details-1
- Uniapp realizes global sharing of wechat applet and custom sharing button style
- For new students, if you have no contact with single-chip microcomputer, it is recommended to get started with 51 single-chip microcomputer
- A lottery like scissors, stone and cloth (C language)
- Stm32f407 key interrupt
- 使用密钥对的形式连接阿里云服务器
- Getting started with JMX, MBean, mxbean, mbeanserver
- 01仿B站项目业务架构
- Leetcode bit operation
- Opencv feature extraction sift
猜你喜欢

Timer and counter of 51 single chip microcomputer

Adaptiveavgpool1d internal implementation

yocto 技术分享第四期:自定义增加软件包支持

SCM is now overwhelming, a wide variety, so that developers are overwhelmed

Yocto Technology Sharing Phase 4: Custom add package support

当你需要使用STM32某些功能,而51实现不了时, 那32自然不需要学

Working mode of 80C51 Serial Port

MySQL root user needs sudo login

LeetCode - 933 最近的请求次数

Cases of OpenCV image enhancement
随机推荐
[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)
单片机学到什么程度能找到工作,这个标准不好量化
Exception handling of arm
QT setting suspension button
LeetCode - 933 最近的请求次数
Basic knowledge of MySQL database (an introduction to systematization)
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
Vgg16 migration learning source code
My 4G smart charging pile gateway design and development related articles
Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
Tensorflow2.0 save model
LeetCode - 895 最大频率栈(设计- 哈希表+优先队列 哈希表 + 栈) *
The underlying principle of vector
(1) What is a lambda expression
使用密钥对的形式连接阿里云服务器
getopt_ Typical use of long function
QT detection card reader analog keyboard input
Vscode markdown export PDF error
Embedded systems are inherently flawed. Compared with the Internet, there are so many holes that it is simply difficult to walk away from
2020-08-23