当前位置:网站首页>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;
}
}边栏推荐
- Alibaba canaladmin deployment and canal cluster Ha Construction
- Dom4j traverses and updates XML
- 分配异常的servlet
- URL backup 1
- Methods of using arrays as function parameters in shell
- 请求参数的发送和接收
- Shell script kills the process according to the port number
- [concurrent programming] explicit lock and AQS
- 单调栈-503. 下一个更大元素 II
- 樹形DP AcWing 285. 沒有上司的舞會
猜你喜欢

LeetCode 75. 颜色分类

状态压缩DP AcWing 291. 蒙德里安的梦想

Binary tree traversal (first order traversal. Output results according to first order, middle order, and last order)

Debug debugging - Visual Studio 2022

Gif remove blank frame frame number adjustment

注解简化配置与启动时加载

Annotations simplify configuration and loading at startup
![[rust notes] 02 ownership](/img/f7/74f8ea3bd697957f9ebfa3e1513fda.png)
[rust notes] 02 ownership

Complex character + number pyramid

Dom4j遍历和更新XML
随机推荐
Location of package cache downloaded by unity packagemanager
How to place the parameters of the controller in the view after encountering the input textarea tag in the TP framework
使用dlv分析golang进程cpu占用高问题
剑指 Offer II 091. 粉刷房子
Parameters of convolutional neural network
On the difference and connection between find and select in TP5 framework
高斯消元 AcWing 883. 高斯消元解线性方程组
22-06-28 Xi'an redis (02) persistence mechanism, entry, transaction control, master-slave replication mechanism
Get the link behind? Parameter value after question mark
[set theory] order relation (total order relation | total order set | total order relation example | quasi order relation | quasi order relation theorem | bifurcation | quasi linear order relation | q
[rust notes] 05 error handling
PHP mnemonic code full text 400 words to extract the first letter of each Chinese character
[concurrent programming] atomic operation CAS
Common DOS commands
Unity multi open script
22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令
[concurrent programming] Table hopping and blocking queue
Apache startup failed phpstudy Apache startup failed
Methods of using arrays as function parameters in shell
第一个Servlet