LeetCode 508. Most Frequent Subtree Sum
原题链接在这里:https://leetcode.com/problems/most-frequent-subtree-sum/description/
题目:
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.
题解:
自下而上计算每个点的sum, 保存这个sum和对应的count. 更新维护最大的count.
Time Complexity: O(n). Space: O(n). hm size.
AC Java:
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 class Solution { 11 int maxCount; 12 HashMap<Integer, Integer> hm; 13 public int[] findFrequentTreeSum(TreeNode root) { 14 maxCount = 0; 15 hm = new HashMap<Integer, Integer>(); 16 17 postOrder(root); 18 19 List<Integer> res= new ArrayList<Integer>(); 20 for(Map.Entry<Integer, Integer> entry : hm.entrySet()){ 21 if(entry.getValue() == maxCount){ 22 res.add(entry.getKey()); 23 } 24 } 25 26 int [] resArr = new int[res.size()]; 27 for(int i = 0; i<res.size(); i++){ 28 resArr[i] = res.get(i); 29 } 30 31 return resArr; 32 } 33 34 private int postOrder(TreeNode root){ 35 if(root == null){ 36 return 0; 37 } 38 39 int left = postOrder(root.left); 40 int right = postOrder(root.right); 41 int val = left + right + root.val; 42 hm.put(val, hm.getOrDefault(val, 0)+1); 43 maxCount = Math.max(maxCount, hm.get(val)); 44 45 return val; 46 } 47 }
转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/8364876.html
LeetCode 508. Most Frequent Subtree Sum相关推荐
- LeetCode—494. 目标和(Target Sum)——分析及代码(Java)
LeetCode-494. 目标和[Target Sum]--分析及代码[Java] 一.题目 二.分析及代码 1. 动态规划 (1)思路 (2)代码 (3)结果 2. 动态规划+节省空间 (1)思路 ...
- Leetcode | Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
- LeetCode 404. 左叶子之和(Sum of Left Leaves)
404. 左叶子之和 404. Sum of Left Leaves LeetCode404. Sum of Left Leaves 题目描述 计算给定二叉树的所有左叶子之和. 示例: 3/ \9 2 ...
- [Leetcode] Binary Tree Maximum Path Sum
这是LeetCode上的一道题目,需要求二叉树中两点路径的最大和.原题是 https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ ...
- leetcode 712. Minimum ASCII Delete Sum for Two Strings | 712. 两个字符串的最小ASCII删除和(暴力递归->傻缓存->DP)
题目 https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/ 题解 经典的 暴力递归 -> 傻缓存 -&g ...
- leetcode 416. Partition Equal Subset Sum | 416. 分割等和子集(Java)
题目 https://leetcode.com/problems/partition-equal-subset-sum/ 题解 乍一看是背包问题,但不同的是,本题要求结果等于某值,背包要求结果小于某值 ...
- leetcode 209. Minimum Size Subarray Sum | 209. 长度最小的子数组(Java)
题目 https://leetcode.com/problems/minimum-size-subarray-sum/ 题解 双指针解法,左指针和右指针在合适的时候向右走,并维护一个sum 版本1 思 ...
- LeetCode 410. Split Array Largest Sum
方法一:DFS+Memoization 某种程度来说本题和 Word Break 很像.直接dfs暴力做肯定会超时,加上memoization即可. dfs(start, m, ...) 表示从ind ...
- Leetcode: Top K Frequent Elements
Given a non-empty array of integers, return the k most frequent elements.For example, Given [1,1,1,2 ...
最新文章
- mysql隐藏密码_MySQL在Linux系统中隐藏命令行中的密码的方法
- c++面向对象高级编程 学习三 堆、栈和内存泄漏
- 美团的DBProxy实践
- 数据结构之排序算法:基础概念
- leetcode - 392. 判断子序列
- python 字符串find方法怎么用_Python字符串find()方法
- linux echo输出转义换行回车引号
- win11如何自动启用禁用设置时区 windows11自动启用禁用设置时区的步骤方法
- 获取当前时间以及模拟倒计时(Java)
- 怎么得到16位校检和-c语言,16位CRC校验C语言算法.pdf
- ReflectionZ_测试_01
- 力特usb转232驱动程序下载_电脑USB接口、U盘接口不能使用的原因及解决方法
- 如何关掉 pyg解密小组声明窗口 (飘云阁番茄插件)
- ogg与wav格式转换
- 2011年课外书 杂书总结感想
- 应用交付学习笔记三-BIG-IP LTM健康检查
- 使用网络调试助手通过MQTT协议接入到华为云物联网平台
- mac下chrome导入证书
- 算法的时间复杂度到底怎么算?
- 2016年终总结,不慌不忙不急不躁的一年