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 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.
我们想下子树有何特点,必须是要有叶结点,单独的一个叶结点也可以当作是子树,那么子树是从下往上构建的,这种特点很适合使用后序遍历,我们使用一个哈希表来建立子树和跟其出现频率的映射,用一个变量cnt来记录当前最多的次数,递归函数返回的是以当前结点为根结点的子树结点值之和,然后在递归函数中,我们先对当前结点的左右子结点调用递归函数,然后加上当前结点值,然后更新对应的哈希表中的值,然后看此时哈希表中的值是否大于等于cnt,大于的话首先要清空res,等于的话不用,然后将sum值加入结果res中即可,参见代码如下:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int count =0;
public int[] findFrequentTreeSum(TreeNode root) {
Map<Integer, Integer> map = new HashMap<>();
List<Integer> list = new ArrayList<>();
postOrder(root, map, list);
int[] res = new int[list.size()];
for(int i = 0; i<list.size(); i++){
res[i] = list.get(i);
}
return res;
} public int postOrder(TreeNode root, Map<Integer, Integer> map, List<Integer> res){
if(root == null){
return 0;
}
int left = postOrder(root.left, map, res);
int right = postOrder(root.right, map, res);
int sum = left+right+root.val;
map.put(sum, map.getOrDefault(sum, 0)+1);
if(map.get(sum) >= count){
if(map.get(sum) > count){
res.clear();
}
res.add(sum);
count = map.get(sum);
}
return sum;
}
}
LeetCode - Most Frequent Subtree Sum的更多相关文章
- [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] 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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [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 ...
- 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 ...
- [leetcode]508. Most Frequent Subtree Sum二叉树中出现最多的值
遍历二叉树,用map记录sum出现的次数,每一个新的节点都统计一次. 遍历完就统计map中出现最多的sum Map<Integer,Integer> map = new HashMap&l ...
- 508 Most Frequent Subtree Sum 出现频率最高的子树和
详见:https://leetcode.com/problems/most-frequent-subtree-sum/description/ C++: /** * Definition for a ...
随机推荐
- js数组及常用数学方法
数组方法 清空数组 1: arr.length=0; 2: arr=[]; arr.push() //往数组最后一个添加元素,会待会一个返回值,就是新的数组长度arr.uns ...
- linux:centOs7换源阿里云
备份: mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 下载: wget -O /etc/y ...
- 數據庫ORACLE轉MYSQL存儲過程遇到的坑~(總結)
ORACLE數據庫轉MySQL數據庫遇到的坑 總結 最近在做Oracle轉mysql的工程,遇到的坑是真的多,尤其是存儲過程,以前都沒接觸過類似的知識,最近也差不多轉完了就總結一下.希望能幫到一些人( ...
- flask项目结构(二)创建flask,同步docker
简介: 建立flask容易,那么部署就比较麻烦了,配这个,配那个,更新………… 所以我从构建,就考虑部署的问题,使用docker部署. 程序都打包进docker,本博客有相关文章. pycharn直接 ...
- java 实现简单的顺序栈
package com.my; import java.util.Arrays; /** * 顺序栈 * @author wanjn * */ public class ArrayStack { pr ...
- Android : 移植curl-7.61.1 及 openssl-1.1.0i
一.curl-7.61.1 Android平台移植: libcurl是一个免费且易于使用的客户端URL传输库,支持DICT.FILE.FTP.FTPS.Gopher.HTTP.HTTPS.IMAP.I ...
- Spring Boot学习笔记----POI(Excel导入导出)
业务:动态生成模板导出Excel,用户修改完再导入Excel. Spring boot + bootstrap + poi 1.添加Dependence <dependency> < ...
- Docker Kubernetes(K8s)简介
入职了新公司,使用了Docker和K8s,需要有一个基础的了解,对网络上相关信息进行了简单总结. 一Docker 1简介: Docker 将应用程序与该程序的依赖,打包在一个文件里面.运行这个文件,就 ...
- 卷积神经网络-Dropout
dropout 是神经网络用来防止过拟合的一种方法,很简单,但是很实用. 基本思想是以一定概率放弃被激活的神经元,使得模型更健壮,相当于放弃一些特征,这使得模型不过分依赖于某些特征,即使这些特征是真实 ...
- C#正则表达式类Match和Group类的理解
正则表达式可以看做一种有特定功能的小型编程语言,在一段文本中定位子字符串.利用正则表达式可以快速地分析大量的文本以找到特定的字符模式:提取.编辑.替换或删除文本子字符串:或将提取的字符串添加到集合.正 ...