当前位置:网站首页>[LeetCode]508. The most frequent subtree elements and
[LeetCode]508. The most frequent subtree elements and
2022-06-27 21:50:00 【A Fei algorithm】
subject
508. The most frequent sub tree elements and
Give you a root node of binary tree root , Please return the most frequent subtree elements and . If more than one element appears the same number of times , Returns all the most frequent subtree elements and ( Unlimited order ).
Of a node 「 Subtree elements and 」 It is defined as the sum of the elements of all nodes in the binary tree with the node as the root ( Including the node itself ).
Example 1:
Input : root = [5,2,-3]
Output : [2,-3,4]
Example 2:
Input : root = [5,2,-5]
Output : [2]
Tips :
The number of nodes is [1, 104] Within the scope of
-105 <= Node.val <= 105
Method 1:DFS
public int[] findFrequentTreeSum(TreeNode root) {
if (root == null) return new int[]{
};
dfs(root);
List<Integer> list = new ArrayList<>();
for (int k : map.keySet()) {
if (map.get(k) == maxx) list.add(k);
}
int[] res = new int[list.size()];
for (int i = 0; i < list.size(); i++) res[i] = list.get(i);
return res;
}
int maxx = 0;// Maximum number of occurrences
// Record the current occurrence of sum The number of times
Map<Integer, Integer> map = new HashMap<>();
private int dfs(TreeNode root) {
if (root == null) return 0;
int l = dfs(root.left);
int r = dfs(root.right);
int s = l + root.val + r;
map.put(s, map.getOrDefault(s, 0) + 1);
maxx = Math.max(maxx, map.get(s));
return s;
}
边栏推荐
- Codeforces Round #717 (Div. 2)
- Knowledge sorting of exception handling
- 100 important knowledge points that SQL must master: sorting and retrieving data
- win11桌面出现“了解此图片”如何删除
- Xiao Wang's interview training task
- Let Ma Huateng down! Web3.0, hopeless
- Go from introduction to actual combat - panic and recover (notes)
- 微服务之远程调用
- 猜拳游戏专题训练
- Go从入门到实战——多态(笔记)
猜你喜欢

Go从入门到实战——仅执行一次(笔记)

Full record of 2022 open source moment at Huawei partners and Developers Conference

Go从入门到实战——Panic和recover(笔记)

互联网 35~40 岁的一线研发人员,对于此岗位的核心竞争力是什么?

MYSQL和MongoDB的分析

SQL必需掌握的100个重要知识点:过滤数据

What is the core competitiveness of front-line R & D personnel aged 35~40 in this position?

开源技术交流丨一站式全自动化运维管家ChengYing入门介绍

Go from introduction to practice - Interface (notes)

At 19:00 on Tuesday evening, the 8th live broadcast of battle code Pioneer - how to participate in openharmony's open source contribution in multiple directions
随机推荐
uniapp拦截请求
Icml2022 | scalable depth Gaussian Markov random field
AI 绘画极简教程
Go从入门到实战——仅执行一次(笔记)
The difference between scrum and Kanban
100 important knowledge points that SQL must master: retrieving data
让马化腾失望了!Web3.0,毫无希望
TypeScript学习
Go从入门到实战——共享内存并发机制(笔记)
Process control task
∫(0→1) ln(1+x) / (x² + 1) dx
[LeetCode]动态规划解分割数组II[Arctic Fox]
动态刷新mapper看过来
Go从入门到实战——任务的取消(笔记)
How to delete "know this picture" on win11 desktop
Full record of 2022 open source moment at Huawei partners and Developers Conference
excel读取文件内容方法
GBase 8a数据库用户密码安全相关参数汇总
Oracle migration MySQL unique index case insensitive don't be afraid
神奇的POI读取excel模板文件报错