当前位置:网站首页>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;
}
}边栏推荐
- 拯救剧荒,程序员最爱看的高分美剧TOP10
- Concurrent programming (III) detailed explanation of synchronized keyword
- Gif remove blank frame frame number adjustment
- Deeply understand the underlying data structure of MySQL index
- Noip 2002 popularity group selection number
- Deep parsing (picture and text) JVM garbage collector (II)
- 20220630学习打卡
- DOM render mount patch responsive system
- Life cycle of Servlet
- Drawing maze EasyX library with recursive backtracking method
猜你喜欢

LeetCode 871. 最低加油次数

Find the combination number acwing 886 Find the combination number II

Arbre DP acwing 285. Un bal sans patron.

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

Low code momentum, this information management system development artifact, you deserve it!

Annotations simplify configuration and loading at startup

分配异常的servlet

网络安全必会的基础知识

请求参数的发送和接收

第一个Servlet
随机推荐
Wonderful review | i/o extended 2022 activity dry goods sharing
Es8 async and await learning notes
The method for win10 system to enter the control panel is as follows:
LeetCode 75. 颜色分类
Methods of using arrays as function parameters in shell
PHP mnemonic code full text 400 words to extract the first letter of each Chinese character
Find the combination number acwing 885 Find the combination number I
On the setting of global variable position in C language
Slice and index of array with data type
Concurrent programming (VI) ABA problems and solutions under CAS
20220630 learning clock in
On a un nom en commun, maître XX.
What is the difference between sudo apt install and sudo apt -get install?
PHP uses foreach to get a value in a two-dimensional associative array (with instances)
Dom4j遍历和更新XML
LeetCode 438. 找到字符串中所有字母异位词
[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
Servlet的生命周期
Use of sort command in shell
Drawing maze EasyX library with recursive backtracking method