Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Most Frequent Subtree Sum problem solution

YASH PAL, 31 July 2024

In this Leetcode Most Frequent Subtree Sum problem solution 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).

Leetcode Most Frequent Subtree Sum problem solution

Problem solution in Python.

class Solution:
    def findFrequentTreeSum(self, root: Optional[TreeNode]) -> List[int]:
        hash = {}
        
        # Traversal DFS
        def getSum(node):
            if node is not None:
                leftSum = getSum(node.left)
                rightSum = getSum(node.right)
                total =  leftSum + node.val + rightSum
                if total in hash:
                    hash[total] +=1
                else:
                    hash[total] = 1
                return total
            else:
                return 0
        getSum(root)
        
        # Find the outputs
        max_l = max(hash.values())
        return [key for key, val in hash.items() if val == max_l]

Problem solution in Java.

class Solution {
    public HashMap<Integer,Integer> hm=new HashMap<>();
    public int[] findFrequentTreeSum(TreeNode root) {
       int max=0;
        
       helper(root);
       for(int i:hm.keySet()){
           max=Math.max(max,hm.get(i));
       }
       List<Integer> ls=new ArrayList<>();
        for(int i:hm.keySet()){
           if(hm.get(i)==max)
               ls.add(i);
       }
        int[] a=new int[ls.size()];
        for(int i=0;i<a.length;i++)
            a[i]=ls.get(i);
        
        return a;
        
    }
    public int helper(TreeNode root){
        if(root==null){
            return 0;
        }
        int val=helper(root.left)+helper(root.right)+root.val;
        hm.put(val,hm.getOrDefault(val,0)+1);
        return val; 
    }
}

Problem solution in C++.

class Solution {
private:
    map<TreeNode*, int> mp;
public:
    vector<int> findFrequentTreeSum(TreeNode* root) {
        int t = get_sum(root), n = 0;
        map<int,int> m;
        vector<int> res;
        for(auto it:mp){
            m[it.second]++;
            n = max(n,m[it.second]);
        }
        for(auto it:m)
            if(it.second == n)
                res.push_back(it.first);
        return res;
    }
    
    int get_sum(TreeNode* root){
        if(root == nullptr)
            return 0;
        if(mp.find(root) != mp.end())
            return mp[root];
        int res = root->val;
        res += get_sum(root->left) + get_sum(root->right);
        mp[root] = res;
        return res;
    }  
};

coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes