当前位置:网站首页>LeetCode 508. 出现次数最多的子树元素和
LeetCode 508. 出现次数最多的子树元素和
2022-07-03 08:49:00 【Sasakihaise_】

【DFS】DFS的过程中用map记录子树和已经出现次数,遍历map找出现次数最多即可。
/**
* 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 {
// 12:47 6
public Map<Integer, Integer> map;
public int dfs(TreeNode root){
if(root == null) return 0;
int left = dfs(root.left);
int right = dfs(root.right);
int sum = root.val + left + right;
map.put(sum, map.getOrDefault(sum, 0) + 1);
return sum;
}
public int[] findFrequentTreeSum(TreeNode root) {
map = new HashMap();
dfs(root);
int max = 0;
List<Integer> list = new ArrayList();
for(var k: map.keySet()){
if(map.get(k) > max){
list.clear();
list.add(k);
max = map.get(k);
}else if(map.get(k) == max){
list.add(k);
}
}
int[] ans = new int[list.size()];
int i = 0;
for(var j: list) ans[i++] = j;
return ans;
}
}边栏推荐
- [MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle
- createjs easeljs
- PHP mnemonic code full text 400 words to extract the first letter of each Chinese character
- MySQL three logs
- Debug debugging - Visual Studio 2022
- 22-05-26 西安 面试题(01)准备
- [concurrent programming] explicit lock and AQS
- [concurrent programming] atomic operation CAS
- Servlet的生命周期
- Summary of methods for counting the number of file lines in shell scripts
猜你喜欢

Redux - learning notes

How to place the parameters of the controller in the view after encountering the input textarea tag in the TP framework

Es8 async and await learning notes

状态压缩DP AcWing 91. 最短Hamilton路径

第一个Servlet

Markdown learning

Arbre DP acwing 285. Un bal sans patron.

Binary tree sorting (C language, char type)
![[concurrent programming] Table hopping and blocking queue](/img/b7/023991a00956e469af855e7a81e126.jpg)
[concurrent programming] Table hopping and blocking queue
![[redis] redis persistent RDB vs AOF (source code)](/img/57/b6a86c49cedee31fc00dc5d1372023.jpg)
[redis] redis persistent RDB vs AOF (source code)
随机推荐
[concurrent programming] atomic operation CAS
LinkedList set
String splicing method in shell
createjs easeljs
【Rust笔记】05-错误处理
Alibaba canaladmin deployment and canal cluster Ha Construction
[concurrent programming] concurrent security
Solution of 300ms delay of mobile phone
Message queue for interprocess communication
Eating fruit
使用dlv分析golang进程cpu占用高问题
[concurrent programming] Table hopping and blocking queue
producer consumer problem
Introduction to the usage of getopts in shell
TP5 order multi condition sort
The difference between if -n and -z in shell
[rust notes] 13 iterator (Part 1)
Summary of methods for counting the number of file lines in shell scripts
Deeply understand the underlying data structure of MySQL index
Location of package cache downloaded by unity packagemanager