当前位置:网站首页>[leetcode] 508. Élément de sous - arbre le plus fréquent et
[leetcode] 508. Élément de sous - arbre le plus fréquent et
2022-06-27 21:49:00 【Algorithme Afi】
Titre
508. Les éléments du sous - arbre les plus fréquents et
Pour vous donner un noeud racine d'arbre binaire root ,Veuillez retourner l'élément sous - arbre qui apparaît le plus souvent et.Si plus d'un élément apparaît le même nombre de fois,Renvoie tous les éléments du sous - arbre les plus fréquents et(Ordre illimité).
Un noeud 「Élément de sous - arbre et」 Défini comme la somme des éléments de tous les noeuds d'un arbre binaire dont le noeud est la racine(Y compris le noeud lui - même).
Exemple 1:
Entrée: root = [5,2,-3]
Produits: [2,-3,4]
Exemple 2:
Entrée: root = [5,2,-5]
Produits: [2]
Conseils:
Nombre de noeuds [1, 104] Dans le champ d'application
-105 <= Node.val <= 105
Méthodes1: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;// Nombre maximum d'occurrences
// Enregistrer ce qui se passe actuellement sum Nombre de fois
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;
}
边栏推荐
- Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
- 01-Golang-环境搭建
- 空指针异常
- Go从入门到实战——CSP并发机制(笔记)
- Little known MySQL import data
- Go from introduction to actual combat -- channel closing and broadcasting (notes)
- Full record of 2022 open source moment at Huawei partners and Developers Conference
- 让马化腾失望了!Web3.0,毫无希望
- Go from introduction to actual combat - panic and recover (notes)
- Tiktok's interest in e-commerce has hit the traffic ceiling?
猜你喜欢

Bit. Store: long bear market, stable stacking products may become the main theme
![[LeetCode]动态规划解分割数组II[Arctic Fox]](/img/a1/4644206db3e14c81f9f64e4da046bf.png)
[LeetCode]动态规划解分割数组II[Arctic Fox]

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

SQL必需掌握的100个重要知识点:排序检索数据

01-Golang-环境搭建

Special training of guessing game

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

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

强制 20 天内开发 APP 后集体被裁,技术负责人怒批:祝“早日倒闭!”

Go从入门到实战——依赖管理(笔记)
随机推荐
TreeSet详解
Go从入门到实战——行为的定义和实现(笔记)
Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
Flask----应用案例
集合代码练习
ICML2022 | 可扩展深度高斯马尔可夫随机场
Is it safe to open an account and buy stocks? Who knows
gomock mockgen : unknown embedded interface
Sharing | intelligent environmental protection - ecological civilization informatization solution (PDF attached)
List of language weaknesses --cwe, a website worth learning
Go from introduction to practice - polymorphism (note)
Special training of guessing game
[LeetCode]513. 找树左下角的值
Go from introduction to actual combat - panic and recover (notes)
Special tutorial - Captain selection game
Go从入门到实战——仅需任意任务完成(笔记)
Go from introduction to practice -- coordination mechanism (note)
我想我要开始写我自己的博客了。
今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献
Let Ma Huateng down! Web3.0, hopeless