508 Most Frequent Subtree Sum 出现频率最高的子树和
详见:https://leetcode.com/problems/most-frequent-subtree-sum/description/
C++:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> findFrequentTreeSum(TreeNode* root) {
vector<int> res;
int cnt=0;
unordered_map<int,int> m;
postorder(root,m,cnt,res);
return res;
}
int postorder(TreeNode* node,unordered_map<int,int> &m,int &cnt,vector<int> &res)
{
if(!node)
{
return 0;
}
int left=postorder(node->left,m,cnt,res);
int right=postorder(node->right,m,cnt,res);
int sum=left+right+node->val;
++m[sum];
if(m[sum]>=cnt)
{
if(m[sum]>cnt)
{
res.clear();
}
res.push_back(sum);;
cnt=m[sum];
}
return sum;
}
};
参考:http://www.cnblogs.com/grandyang/p/6481682.html
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] 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 最频繁的子树和
[抄题]: Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum ...
- 【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]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 ...
随机推荐
- java获取class的几种方式
以获取Hello.class为例 public class Hello { public static void main(String[] args) { // TODO Auto-generate ...
- sessionFactory的创建和四种查询方式
1,关于sessionFactory的创建 5.0版本之前,下面这种方式在5.0及之后,可能会出问题,建议修改为5.0之后的方式 // 实例化Configuration Configuration c ...
- yum 安装 mysql
安装 yum -y install mysql-server 开机自启动 chkconfig mysqld on 设置账户密码 启动 service mysqld start -- 进入mysql库 ...
- All the best open source and Software as a Service (SaaS) tools in one place 工具 工欲善其事必先利其器
Open Source & SaaS Tools | StackShare https://stackshare.io/categories AfterShip/SaaS: List of S ...
- ICMP协议 广播以查询局域网内的所有主机
看到了很多局域网内的主机扫描工具,在想怎么去实现这样一个工具.前几天看了Ping源码--ICMP协议的实例,ICMP可以用来探测网联网内的任一主机,ICMP和广播地址结合来扫描局域网内的所有主机不是很 ...
- 北斗有 35 颗卫星,而 GPS 有 24 颗卫星,为什么二者数量不同?
作者:知乎用户链接:https://www.zhihu.com/question/21092045/answer/17164418来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...
- Linux内核中工作队列的使用work_struct,delayed_work【转】
本文转载自:http://blog.csdn.net/zahuopuboss/article/details/43268983 初始化工作队列 调度工作队列 取消工作队列 #include <l ...
- Why is an 'Any CPU' application running as x86 on a x64 machine?
It's likely that you linked some assemblies that are not Any CPU, but include native code (or are ...
- uestc 250 windy数(数位dp)
题意:不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道,在A和B之间,包括A和B,总共有多少个windy数? 思路:数位dp #include<iostream ...
- Java变量和常量声明
一.变量 1.变量的定义 变量是内存中的一个存储区域,该区域有自己的名称(变量名)和类型(数据类型),Java中每个变量必须先声明,后使用 该区域的数据可以在同一类型范围内 ...