Easy python solution


  • 0
    Z
    from collections import defaultdict
    
    
    class Solution(object):
        
        def findFrequentTreeSum(self, root):
            """
            :type root: TreeNode
            :rtype: List[int]
            """
            if not root: return []
            self.index = defaultdict(int)
            self.maximum = 0
            self.helper(root)
            return [i for i, v in self.index.items() if v == self.maximum]
            
        def helper(self, node):
            if not node:
                return 0
            node.val += self.helper(node.left) + self.helper(node.right)
            self.index[node.val] += 1
            self.maximum = max(self.maximum, self.index[node.val])
            return node.val
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.