遍历二叉树,用map记录sum出现的次数,每一个新的节点都统计一次。

遍历完就统计map中出现最多的sum

Map<Integer,Integer> map = new HashMap<>();
public int[] findFrequentTreeSum(TreeNode root) {
helper(root);
int max = 0;
List<Integer> list = new ArrayList<>();
for (int key : map.keySet()) {
int v = map.get(key);
if (v>max)
{
list.clear();
list.add(key);
max = v;
}
else if (v==max) list.add(key);
}
int[] res = new int[list.size()];
for (int i = 0; i < res.length; i++) {
res[i] = list.get(i);
}
return res;
}
public int helper(TreeNode root)
{
if (root==null) return 0;
int cur = root.val;
cur+= helper(root.left);
cur+=helper(root.right);
map.put(cur,map.getOrDefault(cur,0)+1);
return cur;
}

[leetcode]508. Most Frequent Subtree Sum二叉树中出现最多的值的更多相关文章

  1. [LeetCode] 508. Most Frequent Subtree Sum 出现频率最高的子树和

    Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a ...

  2. 【LeetCode】508. Most Frequent Subtree Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. 508. Most Frequent Subtree Sum 最频繁的子树和

    [抄题]: Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum ...

  4. 508. Most Frequent Subtree Sum

    Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a ...

  5. 508 Most Frequent Subtree Sum 出现频率最高的子树和

    详见:https://leetcode.com/problems/most-frequent-subtree-sum/description/ C++: /** * Definition for a ...

  6. [LeetCode] Most Frequent Subtree Sum 出现频率最高的子树和

    Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a ...

  7. LeetCode - Most Frequent Subtree Sum

    Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a ...

  8. [Swift]LeetCode508. 出现次数最多的子树元素和 | Most Frequent Subtree Sum

    Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a ...

  9. [leetcode-508-Most Frequent Subtree Sum]

    Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a ...

随机推荐

  1. gsap基础一[from,to,fromTo]

    学了几天基础了,感觉总算有点入了一个门的感觉啦,gasp不难,想想一年前我看着官网跟天文一样,今年真的进步很大,在外网发现学习的新世界, 自己的获取知识和查看api源码的能力也增强了许多,现在国内的气 ...

  2. 关于缓存的一些重要概念(Redis 前置菜)

    1. 缓存的基本思想 很多朋友,只知道缓存可以提高系统性能以及减少请求相应时间,但是,不太清楚缓存的本质思想是什么. 缓存的基本思想其实很简单,就是我们非常熟悉的空间换时间.不要把缓存想的太高大上,虽 ...

  3. 第4.2节 神秘而强大的Python生成器精讲

    一. 生成器(generator)概念 生成器是一个特殊的迭代器,它保存的是算法,每次调用next()或send()就计算出下一个元素的值,直到计算出最后一个元素,没有更多的元素时,抛出StopIte ...

  4. Python中sort、sorted的cmp参数废弃之后使用__lt__支持复杂比较的方法

    Python2.1以前的排序比较方法只提供一个cmp比较函数参数,没有__lt__等6个富比较方法, Python 2.1引入了富比较方法,Python3.4之后作废了cmp参数.相应地从Python ...

  5. PyQt(Python+Qt)学习随笔:Qt Designer中部件的windowTitle和windowOpacity属性

    windowOpacity 这个属性仅对window对象生效. windowOpacity为浮点数,表示透明度,为1完全不透明,为0完全透明,缺省是1. 可以通过windowOpacity().set ...

  6. Django 框架基本操作(二)

    一.设计表结构 1.班级表结构 表名:grade 字段:班级名称(gname).成立时间(gdate).女生总数(ggirlnum).男生总数(gboynum).是否删除(isDelete) 2.学生 ...

  7. 攻防世界 web进阶区 ics-06

    攻防世界 ics-06 涉及知识点: (1)php://filter协议 (2)php中preg_replace()函数的漏洞 解析: 进入题目的界面,一通乱点点出了唯一一个可以进入的界面. 观察ur ...

  8. 2020 AC Saber夏季赛 游记

    看着 \(\text{Acwing}\) 成长的我,还是写一下游记吧(?). 刚刚中考完,手速本来就老年,更慢,算法每次写完都要调,而且还查不出错.. Day 1 初赛.紧张,因为中考四五个月没打了, ...

  9. 题解-CF1418G Three Occurrences

    题面 CF1418G Three Occurrences 给一个 \(n\) 个数的序列 \(a_i\),求每个出现过的数出现次数为 \(3\) 的子序列个数. 数据范围:\(1\le n\le 5\ ...

  10. Angular学习知识点记录

    问:版本直接跳转到Angular4? 答:为了遵循严格的版本策略.在angular2.x的时候,angular route的版本已经是版本3了.因此为了版本统一,angular直接从2跳到了4,.参考 ...