Most Frequent Subtree Sum in C++


Suppose we have the root of a tree, we have to find the most frequent subtree sum. The subtree sum of a node is actually the sum of all the node values formed by the subtree rooted at that node (including the node itself). The most frequent subtree sum is actually If there is a tie, return all the values with the highest frequency in any order. So if the tree is like [5,2,-5], then it will return [2]. This is because 2 happens twice, however -5 only occurs once.

To solve this, we will follow these steps −

  • Define two maps m and freq, m is a set of integer key and a corresponding lists, and freq will store the frequency of each numbers.

  • define one method called solve(), this will take the tree node. This will act as follows −

  • if node is null, then return 0

  • leftSum := solve(left of node), rightSum := solve(right of node)

  • currSum := node value + leftSum + rightSum

  • if frequency count is not same as currSum, then

    • insert currSum into the list present at m[1]

    • set freq[currSum] := 1

  • otherwise

    • increase freq[currSum] by 1

    • insert currSum into the list present at m[freq[currSum]]

  • return currSum

  • The main method will be like −

  • if root is null, then return empty set

  • solve(root)

  • return the last list of map m

Let us see the following implementation to get better understanding −

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<auto> v){
   cout << "[";
   for(int i = 0; i<v.size(); i++){
      cout << v[i] << ", ";
   }
   cout << "]"<<endl;
}
class TreeNode{
   public:
   int val;
   TreeNode *left, *right;
   TreeNode(int data){
      val = data;
      left = NULL;
      right = NULL;
   }
};
void insert(TreeNode **root, int val){
   queue<TreeNode*> q;
   q.push(*root);
   while(q.size()){
      TreeNode *temp = q.front();
      q.pop();
      if(!temp->left){
         if(val != NULL)
            temp->left = new TreeNode(val);
         else
            temp->left = new TreeNode(0);
         return;
      }else{
         q.push(temp->left);
      }
      if(!temp->right){
         if(val != NULL)
            temp->right = new TreeNode(val);
         else
            temp->right = new TreeNode(0);
         return;
      } else {
         q.push(temp->right);
      }
   }
}
TreeNode *make_tree(vector<int> v){
   TreeNode *root = new TreeNode(v[0]);
   for(int i = 1; i<v.size(); i++){
      insert(&root, v[i]);
   }
   return root;
}
class Solution {
   public:
   map <int, vector <int> > m;
   map <int, int > freq;
   int solve(TreeNode* node){
      if(!node)return 0;
      int leftSum = solve(node->left);
      int rightSum = solve(node->right);
      int currSum = node->val + leftSum + rightSum;
      //cout << currSum << endl;
      if(!freq.count(currSum)){
         m[1].push_back(currSum);
         freq[currSum] = 1;
         //cout << "Adding " << currSum << " 1" << endl;
      } else {
         freq[currSum]++;
         m[freq[currSum]].push_back(currSum);
      }
      return currSum;
   }
   vector<int> findFrequentTreeSum(TreeNode* root) {
      m.clear();
      freq.clear();
      if(!root)return {};
      solve(root);
      return m.rbegin()->second;
   }
};
main(){
   vector<int> v = {5,2,-5};
   TreeNode *tree = make_tree(v);
   Solution ob;
   print_vector(ob.findFrequentTreeSum(tree));
}

Input

[5,2,-5]

Output

[2, ]

Updated on: 30-Apr-2020

205 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements