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 node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself). So what is the most frequent subtree sum value? If there is a tie, return all the values with the highest frequency in any order.
Examples 1
Input:
5
/ \
2 -3
return [2, -3, 4], since all the values happen only once, return all of them in any order.
Examples 2
Input:
5
/ \
2 -5
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
不知道怎么存储maxCount:单独加个变量就行了,况且加变量也不费事啊。
[英文数据结构或算法,为什么不用别的数据结构或算法]:
要求返回数组,不能直接添加,需要间接添加到List<Integer> res中。
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
int left = postOrder(root.left); 要求前序遍历有返回值的时候,从void类型改成int类型,随机应变。虽然是int型,可以不返回,直接调用。
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
虽然是int型,可以不返回,直接调用。不知道怎么存储maxCount:单独加个变量就行了,况且加变量也不费事啊。
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
Map<Integer, Integer> sumToCount = new HashMap<>();
int maxCount = 0; public int[] findFrequentTreeSum(TreeNode root) {
//initilization: map, list res,[] result, maxCount
List<Integer> res = new ArrayList<Integer>(); postOrder(root); //if equals maxCount,add to list
for (int sum : sumToCount.keySet()) {
if (sumToCount.get(sum) == maxCount) {
res.add(sum);
}
} //add to array
int[] result = new int[res.size()];
for (int i = 0; i < res.size(); i++) {
result[i] = res.get(i);
} //return
return result;
} public int postOrder(TreeNode root) {
//corner case
if (root == null) return 0; //traverse left and right
int left = postOrder(root.left);
int right = postOrder(root.right);
int sum = left + right + root.val; //add sum to map
sumToCount.put(sum, sumToCount.getOrDefault(sum, 0) + 1);
int count = sumToCount.get(sum);
//maintain maxCount
maxCount = Math.max(maxCount, count); //return
return sum;
}
}
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 ...
- 508 Most Frequent Subtree Sum 出现频率最高的子树和
详见:https://leetcode.com/problems/most-frequent-subtree-sum/description/ C++: /** * Definition for 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 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 ...
- [leetcode]508. Most Frequent Subtree Sum二叉树中出现最多的值
遍历二叉树,用map记录sum出现的次数,每一个新的节点都统计一次. 遍历完就统计map中出现最多的sum Map<Integer,Integer> map = new HashMap&l ...
- [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 ...
- [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 - 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 ...
随机推荐
- Dynamics 365 CRM large instance copy
使用CRM 大家想必都做过copy. 从一个instance 复制到另外一个instance. 如果你是Dynamics 365 CRM 用户, 并且你的instance超过500GB,甚至1TB+的 ...
- 1.2.1 Excel如何从身份证号中提取日期
在对应的单元格中我们输入公式: =MID(B3,7,4)&"年"&MID(B3,11,2)&"月"&MID(B3,13,2)&a ...
- 学会数据库读写分离、分表分库——用Mycat
系统开发中,数据库是非常重要的一个点.除了程序的本身的优化,如:SQL语句优化.代码优化,数据库的处理本身优化也是非常重要的.主从.热备.分表分库等都是系统发展迟早会遇到的技术问题问题.Mycat是一 ...
- 2013-7-30 802.1X企业级加密
今天做了U9510的企业级加密标杆测试,写了企业级加密标杆设备的操作指南.最后做到server 2003却出了问题,peap能关联,但是TLS怎么都关联不上.用adb shell查看logcat日志, ...
- PP.io的三个阶段,“强中心”——“弱中心”——“去中心”
什么是PP.io? PP.io是我和Bill发起的存储项目,目的在于为开发者提供一个去中心化的存储和分发平台,能做到更便宜,更高速,更隐私. 当然做去中心化存储的项目也有好几个,FileCoin,Si ...
- 20165312 2017-2018-2《JAVA程序设计》第8周学习总结
20165312 2017-2018-2<JAVA程序设计>第8周学习总结 一.第十二章知识点总结 进程与线程 进程是程序的一次动态执行进程,它对应了从代码加载.执行至执行完毕的一个完整过 ...
- NFS服务配置
FS服务会经常用于在网络上共享存储. 比如有3台机子A,B,C;他们都需要访问同一个目录,使用NFS, 只需要把图片都放在A上,然后A共享给B和C即可. 访问B和C时,是通过网络的方式访问A上的哪个目 ...
- Chrome 插件安装技巧
参考http://blog.csdn.net/shiyaru1314/article/details/49303317 最近在学习WEBAPI 由于没有界面可以调试,需要安装Chrome中的插件 P ...
- redis排序
1.sort 排序最常见的是sort命令,可以对列表或者有序集合排序,最简单的排序方式如下: > lpush list (integer) > lpush list (integer) & ...
- OpenCV使用BGR而非RGB格式
日常开发和交流时,我们习惯将图片的颜色通道按照RGB(red, green, blue)进行排列.闲来无聊,在把玩卷积神经网络模型时发现OpenCV并没有按照这个顺序读入图片.好奇地搜索一下OpenC ...