human
I found an interesting problem on https://leetcode.com/problems/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.

I tried it in Python, but could not do it. Can you solve it?

 What is a Python program to find the most frequent subtree sum?
assistant
#Here is Python program to find the most frequent subtree sum.

from collections import defaultdict
 # Definition for a binary tree node.
 # class TreeNode:
 #     def __init__(self, x):
 #         self.val = x
 #         self.left = None
 #         self.right = None
 
 class Solution:
     """
     :type root: TreeNode
     :rtype: map{sum:int : frequency:int} 
     """
     def __init__(self):
         self._iter = 0
     def getAllSums(self, root):
         if not root:
             return defaultdict(int), 0
         all_sums, left_sum = self.getAllSums(root.left)
         all_right_sums, right_sum = self.getAllSums(root.right)    
         # mege both maps
         for key,val in all_right_sums.items():
             all_sums[key] += val
         cur_sum = right_sum + root.val + left_sum
         all_sums[cur_sum] += 1
         return all_sums, cur_sum
         
     def findFrequentTreeSum(self, root):
         """
         :type root: TreeNode
         :rtype: List[int]
         """
         all_sums,_ = self.getAllSums(root)
         result = []
         max_occurence = 0
         for key,val in all_sums.items():
             max_occurence = max(max_occurence,val)
         for key,val in all_sums.items():
             if val == max_occurence:
                 result.append(key)
         return result