遍历二叉树,用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二叉树中出现最多的值的更多相关文章

  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 解题报告(Python & C++)

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

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

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

  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 of a ...

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

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

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

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

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

随机推荐

  1. C#中的WinForm问题——使用滚动条时页面闪烁及重影问题

    当使用鼠标进行滚动查看页面时,由于页面会频繁刷新,如果页面中控件较多会导致页面出现闪烁.重影等问题,如下图所示: 在网上搜索过该问题,大部分都说使用双缓冲可以解决此类问题,即通过设置DoubleBuf ...

  2. 解决调用WebService报基础连接已经关闭: 服务器关闭了本应保持活动状态的连接的错误的方法

    问题可能原因之一:网速的快慢,我经过测试,如果外网访问的话网速慢就是出现此类问题,但是我没有精确测出当在网络流量最低在什么情况下可以避免此类问题问题可能之二:程序发布之前没把原引用的web servi ...

  3. PyQt(Python+Qt)学习随笔:QTableWidget项编辑方法editItem、openPersistentEditor

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 1.触发编辑项的editItem方法 QTableWidget提供了触发项编辑的方法,调用语法如下: ...

  4. 问题:PyCharm调试方法smart step into的用途

    smart step into为智能单步跟踪,当一行代码中有多个函数,想进入其中一个函数调测其他函数不进入调测时,使用该功能可以让调试人员选择进入的函数.如: 就可以选择需要调试进入的函数而其他两个函 ...

  5. 第三章 、使用Qt Designer进行GUI设计

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.Designer的界面功能介绍 进入Qt Designer以后,打开或新建一个ui文件,Qt D ...

  6. Android的intent

    title: Android基础01 date: 2020-02-15 17:17:04 tags: 1.Intent Intent可以让活动进行跳转.使用方式有两种,一种是显式,另一种是隐式. 1. ...

  7. python安装Scrapy框架

    看到自己写的惨不忍睹的爬虫,觉得还是学一下Scrapy框架,停止一直造轮子的行为 我这里是windows10平台,python2和python3共存,这里就写python2.7安装配置Scrapy框架 ...

  8. CSS常用语法缩写

    使用缩写可以帮助减少你CSS文件的大小,更加容易阅读.CSS常用语法缩写的主要规则如下: 颜色 16进制的色彩值,如果每两位的值相同,可以缩写一半,例如:#000000可以缩写为#000;#33669 ...

  9. Codeforces Edu Round 48 A-D

    A. Death Note 简单模拟,可用\(\%\)和 \(/\)来减少代码量 #include <iostream> #include <cstdio> using nam ...

  10. Java并发编程的艺术(二)——volatile、原子性

    什么是volatile Java语言允许线程访问共享变量,为了确保共享变量能够被准确一致地更新,如果一个字段被声明为volatile,那么Java内存模型将会确保所有线程看到这个变量时值是一致的.保证 ...