508. Most Frequent Subtree Sum LeetCode Solution
In this guide, you will get 508. Most Frequent Subtree Sum LeetCode Solution with the best time and space complexity. The solution to Most Frequent Subtree Sum problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Most Frequent Subtree Sum solution in C++
- Most Frequent Subtree Sum solution in Java
- Most Frequent Subtree Sum solution in Python
- Additional Resources

Problem Statement of Most Frequent Subtree Sum
Given the root of a binary tree, return the most frequent subtree sum. If there is a tie, return all the values with the highest frequency in any order.
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).
Example 1:
Input: root = [5,2,-3]
Output: [2,-3,4]
Example 2:
Input: root = [5,2,-5]
Output: [2]
Constraints:
The number of nodes in the tree is in the range [1, 104].
-105 <= Node.val <= 105
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
508. Most Frequent Subtree Sum LeetCode Solution in C++
class Solution {
public:
vector<int> findFrequentTreeSum(TreeNode* root) {
vector<int> ans;
unordered_map<int, int> count;
int maxCount = 0;
sumDownFrom(root, count);
for (const auto& [_, freq] : count)
maxCount = max(maxCount, freq);
for (const auto& [sum, freq] : count)
if (freq == maxCount)
ans.push_back(sum);
return ans;
}
private:
int sumDownFrom(TreeNode* root, unordered_map<int, int>& count) {
if (root == nullptr)
return 0;
const int sum = root->val + sumDownFrom(root->left, count) +
sumDownFrom(root->right, count);
++count[sum];
return sum;
}
};
/* code provided by PROGIEZ */
508. Most Frequent Subtree Sum LeetCode Solution in Java
class Solution {
public int[] findFrequentTreeSum(TreeNode root) {
List<Integer> ans = new ArrayList<>();
Map<Integer, Integer> count = new HashMap<>();
int maxCount = 0;
sumDownFrom(root, count);
for (final int freq : count.values())
maxCount = Math.max(maxCount, freq);
for (final int sum : count.keySet())
if (count.get(sum) == maxCount)
ans.add(sum);
return ans.stream().mapToInt(Integer::intValue).toArray();
}
private int sumDownFrom(TreeNode root, Map<Integer, Integer> count) {
if (root == null)
return 0;
final int sum = root.val + sumDownFrom(root.left, count) + sumDownFrom(root.right, count);
count.merge(sum, 1, Integer::sum);
return sum;
}
}
// code provided by PROGIEZ
508. Most Frequent Subtree Sum LeetCode Solution in Python
class Solution:
def findFrequentTreeSum(self, root: TreeNode | None) -> list[int]:
if not root:
return []
count = collections.Counter()
def dfs(root: TreeNode | None) -> int:
if not root:
return 0
summ = root.val + dfs(root.left) + dfs(root.right)
count[summ] += 1
return summ
dfs(root)
maxFreq = max(count.values())
return [summ for summ in count if count[summ] == maxFreq]
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.