当前位置:网站首页>LeetCode 508. The most frequent subtree elements and
LeetCode 508. The most frequent subtree elements and
2022-07-03 09:01:00 【Sasakihaise_】
508. The most frequent sub tree elements and

【DFS】DFS Used in the process of map Record the subtree and the number of occurrences , Traverse map Find the most frequent occurrences .
/**
* 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;
}
}边栏推荐
- JS non Boolean operation - learning notes
- Concurrent programming (III) detailed explanation of synchronized keyword
- 注解简化配置与启动时加载
- Annotations simplify configuration and loading at startup
- Analysis of Alibaba canal principle
- Method of intercepting string in shell
- String splicing method in shell
- What is the difference between sudo apt install and sudo apt -get install?
- ES6 promise learning notes
- How to place the parameters of the controller in the view after encountering the input textarea tag in the TP framework
猜你喜欢

I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made

Log4j2 vulnerability recurrence and analysis

树形DP AcWing 285. 没有上司的舞会

请求参数的发送和接收

我們有個共同的名字,XX工

Find the combination number acwing 886 Find the combination number II

Binary to decimal, decimal to binary

PHP uses foreach to get a value in a two-dimensional associative array (with instances)

excel一小时不如JNPF表单3分钟,这样做报表,领导都得点赞!

Notes and bugs generated during the use of h:i:s and y-m-d
随机推荐
低代码起势,这款信息管理系统开发神器,你值得拥有!
Concurrent programming (V) detailed explanation of atomic and unsafe magic classes
Parameters of convolutional neural network
Methods of checking ports according to processes and checking processes according to ports
Find the combination number acwing 886 Find the combination number II
Life cycle of Servlet
LeetCode 1089. 复写零
XPath实现XML文档的查询
How to use Jupiter notebook
Shell script kills the process according to the port number
Es8 async and await learning notes
[rust notes] 02 ownership
Binary to decimal, decimal to binary
干货!零售业智能化管理会遇到哪些问题?看懂这篇文章就够了
Format - C language project sub file
22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
ES6 promise learning notes
传统企业数字化转型需要经过哪几个阶段?
第一个Servlet
URL backup 1