[leetcode]508. Most Frequent Subtree Sum二叉树中出现最多的值
遍历二叉树,用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二叉树中出现最多的值的更多相关文章
- [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 ...
- 【LeetCode】508. Most Frequent Subtree Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 508. Most Frequent Subtree Sum 最频繁的子树和
[抄题]: Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum ...
- 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 ...
- 508 Most Frequent Subtree Sum 出现频率最高的子树和
详见:https://leetcode.com/problems/most-frequent-subtree-sum/description/ C++: /** * Definition for a ...
- [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 ...
- 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 ...
- [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 ...
- [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 ...
随机推荐
- 你的Idea还可用吗?不妨试试这个神器!
@ 目录 一.STS安装 1.STS下载 2.STS安装 二.STS使用 1.STS配置JDK 2.STS配置Maven 3.使用STS创建SpringBoot项目 三.优化STS 1.主题美化 2. ...
- scentos7安装redis,以及redis的主从配置
redis的安装 下载redis安装包 wget http://download.redis.io/releases/redis-4.0.6.tar.gz 解压压缩包 tar -zxvf redis- ...
- 从JMM透析volatile与synchronized原理,图文并茂
在面试.并发编程.一些开源框架中总是会遇到 volatile 与 synchronized .synchronized 如何保证并发安全?volatile 语义的内存可见性指的是什么?这其中又跟 JM ...
- PyQt学习随笔:Model/View开发时在view数据项中设置不同角色数据的方法
在往Model中通过QStandardItem等类插入数据项时,除了实际插入的存储数据,还可以设置不同角色(请参考<PyQt学习随笔:Model/View中诸如DisplayRole的数据角色及 ...
- PyQt(Python+Qt)学习随笔:Designer(设计师)中部件属性编辑的cursor(光标样式)属性
部件(又称为组件或控件)的cursor属性保存该部件的鼠标光标形状,当鼠标位于该部件上时就会呈现该属性设置的光标形状,对应类型为枚举类型Qt.CursorShape,可取值的范围可以在Qt文档官网:h ...
- 搭建xss-platform平台
一直想搭在公网搭建自己的XSS平台用来验证XSS漏洞,使用别人的平台自己心里总会有担心被摘果子的顾虑,前几天参考了不少前人的博客,终于搭建好了,搭建的途中也遇到了不少坑,故把搭建的经验分享出来,大佬轻 ...
- 获取radio的值及重置radio
获取:$('input[name=age]:checked').val(); 重置:$('input:radio[name=age]').prop('checked',false);
- REHの个人主页
朝暮与年岁并往 然后与你一同行至天光. 简介 这是怎么做到的啊-- 把那些迷茫的浑浊的不可预知的,裁剪,变化,像个造物主一样,最终成为混沌而又分明的,除去一身的戾气和险恶,把那些复杂和晦涩都剖析成它精 ...
- CSP-S2020复赛游记
[本文经过删改] 前一个月 没做什么 NOIP 的题,感觉这些题对我这个做黄题封顶的人不是很友好. 前一天 考了场模拟赛,全场最低分 89,感觉信心满满. 退役那天 到了 XJ,发现没人可以面基,想着 ...
- MarkDown的练习_Java开发学习路径
MarkDown的练习 语言学习 C/C++语言 Java语言 基础四大件 数据结构与算法 操作系统 计算机网络 设计模式 数据库/SQL 私人令牌:42bb654f53941d5692e98b35f ...