508. Most Frequent Subtree Sum (Medium)

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.

题意:对于给定的一棵二叉树,找出现频率最高的子树之和。

思路:遍历和collections.Counter()计数器

# Definition for a binary tree node.
# class TreeNode():
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution():
    def findFrequentTreeSum(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        cnt = collections.Counter()

        def sumOfSubTree(root):
            if not root:
                return 0
            return root.val + sumOfSubTree(root.left) + sumOfSubTree(root.right)

        def traverseTree(root):
            if not root:
                return None
            cnt[sumOfSubTree(root)] += 1
            traverseTree(root.left)
            traverseTree(root.right)
        traverseTree(root)
        max_sum = max(cnt.values() + [None])
        return [e for e, v in cnt.iteritems() if v == max_sum]
时间: 2024-10-19 19:13:59

508. Most Frequent Subtree Sum (Medium)的相关文章

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

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

遍历二叉树,用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 : m

508.&#160;Most Frequent Subtree Sum

class Solution { int count = 0; HashMap<Integer, Integer> map = new HashMap<>(); // map and count all must be here to be accessible from the functions below public int[] findFrequentTreeSum(TreeNode root) { subSum(root); List<Integer> re

[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

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

LeetCode 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 subtr

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

[LeetCode] 039. Combination Sum (Medium) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 039. Combination Sum (Medium) 链接: 题目:https://leetcode.com/problems/combination-sum/ 代码(github):https://github.com/illuz/leetcode 题意: 给出一些正整数集合,以及一个目标数,从集合中选择一

[array] leetcode - 39. Combination Sum - Medium

leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from