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.

Note: You may assume the sum of values in any subtree is in the range of 32-bit signed integer.

题目含义:给一棵树,找出现次数最多的子树和。子树和就是一个结点以及它下方所有结点构成的子树的总和,在这些总和中找一个出现次数最多的结果,如果有很多个这样的结果,返回这些结果构成的数组

 1     private int maxCount = 0;
2 private Map<Integer,Integer> sumMap = new HashMap<>();
3 private int getNodeSum(TreeNode root) {
4 if (root == null) return 0;
5 int sum = root.val + getNodeSum(root.left) +getNodeSum(root.right);
6 int newCount = sumMap.getOrDefault(sum,0)+1;
7 sumMap.put(sum,newCount);
8 maxCount = Math.max(maxCount,newCount);
9 return sum;
10 }
11
12 public int[] findFrequentTreeSum(TreeNode root) {
13 getNodeSum(root);
14 List<Integer> sums = new ArrayList<>();
15 for (Map.Entry<Integer,Integer> entry:sumMap.entrySet())
16 {
17 if (entry.getValue() == maxCount)
18 sums.add(entry.getKey());
19 }
20 int[] result = new int[sums.size()];
21 for (int i=0;i<sums.size();i++)
22 {
23 result[i] = sums.get(i);
24 }
25 return result;
26 }

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. 508. Most Frequent Subtree Sum 最频繁的子树和

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

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

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

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

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

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

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

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

  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 - 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. signal 信号

    python学习笔记--信号模块signal 阅读目录(Content) 1 signal基本信号名 2 常用信号处理函数 2.1 设置发送SIGALRM信号的定时器 2.2 设置信号处理函数 3 常 ...

  2. let, const用法以及与var的区别

    let 定义变量; 作用域:全局作用域,块级作用域(简单说,是指一对大括号{}): 无预解析机制(简单说,变量需要在声明的位置后面使用): 不可以重复定义. const 定义常量: 作用域: 块级作用 ...

  3. java判断一个字符串是否为数字(整型、int)

    引入commons-lang 的jar包 /** * 判断是否是数字类型 * @param str * @return 如果为空返回false 匹配返回true */ public static bo ...

  4. ARTS Week 18

    Algorithm 本周的 LeetCode 题目为 55. 跳跃游戏 给定一个非负整数数组 nums, 你最初位于数组的 第一个下标 .数组中的每个元素代表你在该位置可以跳跃的最大长度.判断你是否能 ...

  5. 【LeetCode】572. 另一个树的子树 Subtree of Another Tree(Python & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:先序遍历 方法二:DFS + DFS 方法三 ...

  6. 【LeetCode】5. Longest Palindromic Substring 最长回文子串

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:最长回文子串,题解,leetcode, 力扣,python ...

  7. Robot(hdu5673)

    Robot Accepts: 92 Submissions: 188 Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 ...

  8. Understanding Black-box Predictions via Influence Functions

    目录 概 主要内容 样本重要性分析 样本摄动对损失的影响 高效计算 共轭梯度 随机估计 一些应用 附录 (1)的证明 Koh P W, Liang P. Understanding black-box ...

  9. Arm64架构下编译便携Python

    这段时间,我一直忙于将 Rainbond 源码构建模块移植到 Arm64/aarch64 架构中.对于 Python 项目而言,可以直接通过源代码编译成为可运行在各种容器平台之上的容器镜像.这个过程不 ...

  10. 生成对抗网络GAN与DCGAN的理解

    作者在进行GAN学习中遇到的问题汇总到下方,并进行解读讲解,下面提到的题目是李宏毅老师机器学习课程的作业6(GAN) 一.GAN 网络上有关GAN和DCGAN的讲解已经很多,在这里不再加以赘述,放几个 ...