<p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>

<p>The <strong>subtree sum</strong> 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).</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li>
	<li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li>
</ul>

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def findFrequentTreeSum(self, root: Optional[TreeNode]) -> List[int]:
        from collections import Counter
        if not root:
            return []
        self.answer = Counter()
        def traverse(node):
            if not node:
                return 0
            prev_sum_left = traverse(node.left)
            prev_sum_right = traverse(node.right)
            sum_subtree = prev_sum_left + prev_sum_right + node.val
            if sum_subtree in self.answer:
                self.answer[sum_subtree] += 1
            else:
                self.answer[sum_subtree] = 1
            return sum_subtree
        if root.left:
            sum_left = traverse(root.left)
        else:
            sum_left = 0
        if root.right:
            sum_right = traverse(root.right)
        else:
            sum_right = 0
        
        sum_subtree = sum_left + sum_right + root.val
        if sum_subtree in self.answer:
            self.answer[sum_subtree] += 1
        else:
            self.answer[sum_subtree] = 1
        most_common_times = self.answer.most_common(1)[0][1]
        if most_common_times == 1:
            answer = [key for key in self.answer.keys()]
            return answer
        else:
            answer = []
            for key,value in self.answer.items():
                if value == most_common_times:
                    answer.append(key)
            return answer
18800