---
# System prepended metadata

title: 0508. Most Frequent Subtree Sum
tags: [Leetcode, Tree, DFS, Medium]

---

# 0508. Most Frequent Subtree Sum
###### tags: `Leetcode` `Medium` `Tree` `DFS`
Link: https://leetcode.com/problems/most-frequent-subtree-sum/description/
## Code
```python=
class Solution:
    def findFrequentTreeSum(self, root: Optional[TreeNode]) -> List[int]:
        if root is None: return []

        def dfs(node):
            if node is None: return 0
            sum = dfs(node.left)+node.val+dfs(node.right)
            count[sum]+=1
            return sum
        
        count = collections.Counter()
        dfs(root)
        maxCount = max(count.values())
        return [s for s in count.keys() if count[s]==maxCount]
```