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
return [2], since 2 happens twice, however -5 only occur once.
思路:
用一个map记录结点的sum与出现的次数。
int sumofTree(TreeNode* root, map<int, int>&counts, int & maxcount)
{
if (root == NULL)return ;
int sum = root->val;
sum += sumofTree(root->left, counts, maxcount);
sum += sumofTree(root->right, counts, maxcount);
counts[sum]++;
maxcount = max(maxcount, counts[sum]);
return sum;
}
vector<int> findFrequentTreeSum(TreeNode* root)
{
vector<int>ret;
map<int, int>counts;
int maxcount = ;
sumofTree(root, counts, maxcount); for (auto it = counts.begin(); it != counts.end();it++)
{
if (it->second == maxcount)ret.push_back(it->first);
}
return ret;
}
参考:
https://discuss.leetcode.com/topic/77763/short-clean-c-o-n-solution
[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二叉树中出现最多的值
遍历二叉树,用map记录sum出现的次数,每一个新的节点都统计一次. 遍历完就统计map中出现最多的sum Map<Integer,Integer> map = new HashMap&l ...
-
【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 ...
随机推荐
-
ANTLR3完全参考指南读书笔记[02]
前言 程序语言是什么? 用wiki上的描述,程序语言是一种人工设计的语言,用于通过指令与机器交互:程序语言是编程程序的标记,而程序是一种计算或算法的描述.详细介绍和背景信息参考: Programmin ...
-
openSUSE13.1 Yast 中所有软件图形化界面无法打开,问题原因: Ruby
因为使用rvm安装了新的Ruby,而openSUSE13.1的YaST又是用Ruby的.....解决方案暂时没有
-
2、onclickListener冲突
事情是这样的. 我在activity中同时使用普通按钮和对话框按钮,并都设置点击时候的回调函数,由于都要用到onclickListener,但是两者却不是一个文件,无法同时import,这就是本文出现 ...
-
RDIFramework.NET(.NET快速信息化系统开发框架) Web版介绍
RDIFramework.NET(.NET快速信息化系统开发框架) Web版介绍 B/S结构(Browser/Server,浏览器/服务器模式),是WEB兴起后的一种网络结构模式,WEB浏览器是客户 ...
-
MySQL常用命令总结3
id SMALLINT UNSIGNED [AUTO_INCREMENT] PRIMARY KEY, //把id定义为主键且自动排号,每张数据表只有一个主键,不能为NULL,确保记录唯一性 //省略a ...
-
Python/ MySQL练习题(一)
Python/ MySQL练习题(一) 查询“生物”课程比“物理”课程成绩高的所有学生的学号 SELECT * FROM ( SELECT * FROM course LEFT JOIN score ...
-
[JLOI 2011]飞行路线&;[USACO 09FEB]Revamping Trails
Description Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并 ...
-
PE知识复习之PE的两种状态
PE知识复习之PE的两种状态 一丶熟悉PE的整体结构 从下面依次网上看.可以得出PE结构 其中DOS头有DOS头结构 也就是 IMAGE_DOS_HEADER 关于结构体的各项属性.前边已经写过了.本 ...
-
Nowcoder contest 392 I 逛公园 (无向图割边模板)
<题目链接> 题目描述: 月月和华华一起去逛公园了.公园很大,为了方便,可以抽象的看成一个N个点M条边的无向连通图(点是景点,边是道路).公园唯一的入口在1号点,月月和华华要从这里出发,并 ...
-
用c语言实现http请求
用c语言来实现一个简单的http请求,请求 www.cnblogs.com 首页 #include<stdio.h> #include<stdlib.h> #include&l ...