
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
#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, ]
- Related Questions & Answers
- Program to find most frequent subtree sum of a binary tree in Python
- Most Frequent Number in Intervals in C++
- Most frequent element in an array in C++
- C# program to find the most frequent element
- Program to find second most frequent character in C++
- Find Second most frequent character in array - JavaScript
- Second most frequent character in a string - JavaScript
- Finding the second most frequent character in JavaScript
- Find largest subtree sum in a tree in C++
- Find most frequent element in a list in Python
- Find the second most frequent element in array JavaScript
- Python program for most frequent word in Strings List
- Inverted Subtree in C++
- Finding n most frequent words from a sentence in JavaScript
- Python program to find Most Frequent Character in a String