当前位置:网站首页>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;
}
}边栏推荐
- 干货!零售业智能化管理会遇到哪些问题?看懂这篇文章就够了
- [concurrent programming] thread foundation and sharing between threads
- LeetCode 57. 插入区间
- LeetCode 241. 为运算表达式设计优先级
- The method for win10 system to enter the control panel is as follows:
- Alibaba canaladmin deployment and canal cluster Ha Construction
- Log4j2 vulnerability recurrence and analysis
- Concurrent programming (V) detailed explanation of atomic and unsafe magic classes
- Mortgage Calculator
- Education informatization has stepped into 2.0. How can jnpf help teachers reduce their burden and improve efficiency?
猜你喜欢

Alibaba canal actual combat

Format - C language project sub file

Binary tree sorting (C language, char type)

On a un nom en commun, maître XX.

Log4j2 vulnerability recurrence and analysis

SQL statement error of common bug caused by Excel cell content that is not paid attention to for a long time

LeetCode 57. 插入区间

第一个Servlet

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

Allocation exception Servlet
随机推荐
22-05-26 Xi'an interview question (01) preparation
URL backup 1
分配异常的servlet
PHP mnemonic code full text 400 words to extract the first letter of each Chinese character
22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令
Low code momentum, this information management system development artifact, you deserve it!
[concurrent programming] thread foundation and sharing between threads
Dom4j traverses and updates XML
干货!零售业智能化管理会遇到哪些问题?看懂这篇文章就够了
createjs easeljs
Dom4j遍历和更新XML
即时通讯IM,是时代进步的逆流?看看JNPF怎么说
Deeply understand the underlying data structure of MySQL index
MySQL index types B-tree and hash
php public private protected
[rust notes] 12 closure
PHP function date (), y-m-d h:i:s in English case
Binary to decimal, decimal to binary
LeetCode 57. 插入区间
[rust notes] 08 enumeration and mode