[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 ...
随机推荐
- rest-framework:频率控制
一 频率简介: 为了控制用户对某个url请求的频率,比如,一分钟以内,只能访问三次 二 自定义频率类,自定义频率规则: 自定义的逻辑 #(1)取出访问者ip # (2)判断当前ip不在访问字典里,添加 ...
- C#(二)基础篇—操作符
2020-12-02 本随笔为个人复习巩固知识用,多从书上总结与理解得来,如有错误麻烦指正 1.数学操作符 int a=2,b=3,c=0; float d=0; c=a+b; //c=5 c++; ...
- docker centos容器无法yum
问题 dockerfile yum -y install vim的时候一直未响应 但是在本地虚拟机centos7上运行则没问题 https://blog.csdn.net/jimiao_xxx ...
- 第4.8节 三目运算、del和pass语句
一.三目运算 Python的三目运算与C语言的三目运算非常类似,具体语法如下: 条件为真的赋值表达式 if 条件 else 条件为假的表达式 三目运算实际上就是一种表达式计算,当对应if后面 ...
- PyQt(Python+Qt)学习随笔:Designer中属性设置界面的属性字体使用粗黑体的含义
老猿Python博文目录 老猿Python博客地址 使用了好几个月的Designer,今天才发现属性编辑界面的属性名有的为粗而黑,有的则不是,如图: 稍微测试了一下,发现是对属性值进行过调整,不再是缺 ...
- 第七章、PyQt图形界面应用程序的事件捕获方法
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一. 概述 PyQt的图形界面应用中,事件处理类似于Windows系统的消息处理.一个带图形界面的应 ...
- PyQt(Python+Qt)学习随笔:Qt Designer中的menu菜单及menu bar菜单栏
菜单由menu bar菜单栏和menu菜单两部分构成,分别对应类QMenuBar和QMenu. menuBar是包含一系列下拉菜单项组成,menu包含两种,一种是直接对应Action的,一种是父菜单, ...
- JMeter断言/检查点
断言就类似LoadRunner中的检查点.对上一个请求返回的信息,获取部分字符串.图片等做判断,确保返回的信息的准确性. 右键点击"HTTP请求" -> "添加&q ...
- neo4j数据库数据转移,从阿里云转移到windows服务器
1.从阿里云迁移neo4j时需停掉neo4j数据库,在neo4j的bin目录下输入 ./neo4j stop 2.将数据备份到一个文件中 ./neo4j-admin dump --database=g ...
- 【CSP-S 2019】树的重心(重心的性质)
Description 给定一颗 \(n\) 个顶点的树 \(\text T\),共 \(n-1\) 次断边操作,每次将树分为两部分 \(\text T_1, \text T_2\),求: \[\su ...