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]的更多相关文章

  1. [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 ...

  2. [leetcode]508. Most Frequent Subtree Sum二叉树中出现最多的值

    遍历二叉树,用map记录sum出现的次数,每一个新的节点都统计一次. 遍历完就统计map中出现最多的sum Map<Integer,Integer> map = new HashMap&l ...

  3. 【LeetCode】508. Most Frequent Subtree Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. 508. Most Frequent Subtree Sum 最频繁的子树和

    [抄题]: Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum ...

  5. 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 ...

  6. 508 Most Frequent Subtree Sum 出现频率最高的子树和

    详见:https://leetcode.com/problems/most-frequent-subtree-sum/description/ C++: /** * Definition for a ...

  7. [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 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. CVE-2014-0038内核漏洞原理与本地提权利用代码实现分析 作者:seteuid0

    关键字:CVE-2014-0038,内核漏洞,POC,利用代码,本地提权,提权,exploit,cve analysis, privilege escalation, cve, kernel vuln ...

  2. ThreadGroup详解

     ①定义线程组 ThreadGroup类中有 2个构造方法,它们用来定义线程组.这 2个构造方法的使用格 式如下: public ThreadGroup(String name); public Th ...

  3. kafka的高可用和一致性探究

    一.kafka基础 本篇文章讨论的kafka版本是目前最新版 0.10.1.0. 1.1 kafka种的KafkaController 所有broker会通过ZooKeeper选举出一个作为Kafka ...

  4. 使用xftp将文件上传至云服务器

    一.在云服务器配置FTP服务:    1.在root权限下,通过如下命令安装Vsftp(以centos 系统为例): yum install -y vsftpd.    2. 在启动vsftpd服务之 ...

  5. CentOS-Zabbix-agent客户端的编译安装

    系统环境: CentOS 6.7 官网下载安装包:http://www.zabbix.com/download 本文用的是Zabbix 3.0 LTS 上传至客户端服务器并解压 .tar.gz 进入解 ...

  6. H5与客户端联调

    进行H5移动端开发时,我们可以使用谷歌浏览器的设备工具栏进行,此方法实时方便快速,但这也是有限的,当我们需要在特定机型特定系统或者在webview中调试时,这种方法就显得很吃力了. 安卓: 一.与安卓 ...

  7. 对clear float 的理解

    之前自己对于清除浮动的用法比较模糊 ,如果用到的话,一般都是采用简单粗暴的方式解决,就是直接用overflow:hidden,但是越用久就会发现其实有BUG,这个BUG正是overflow:hidde ...

  8. Plotting trees from Random Forest models with ggraph

    Today, I want to show how I use Thomas Lin Pederson's awesome ggraph package to plot decision trees ...

  9. js实现省市区三级联动

    电商平台或者一些网站的个人信息部分,通常会有填写地址的功能.该功能一般分为二级联动(省.市)和三级联动(省.市.区),只需要JavaScript就可以实现. 这里介绍一种很简洁易用的方法.参考地址:h ...

  10. Java IO流之随机读写流RandomAccessFile

    随机读写流RandomAccessFile 简介 此类的实例支持对随机访问文件的**读取和写入**. 随机访问文件的行为类似存储在文件系统中的一个大型 byte 数组. 存在指向该隐含数组的光标或索引 ...