# Easy to understand C++ solution using map

• ``````/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
int _findFrequentTreeSum(TreeNode* root, unordered_map<int,int>& map, int& max_val)
{
if(root == NULL)
return 0;

int l = _findFrequentTreeSum(root->left,map,max_val);
int r = _findFrequentTreeSum(root->right,map,max_val);

int val = l+r+root->val;

map[val]++;

if(map[val] > max_val)
max_val = map[val];

return l+r+root->val;
}
public:
vector<int> findFrequentTreeSum(TreeNode* root) {

unordered_map<int,int> map;
int max_val = 0;
vector<int> result;

_findFrequentTreeSum(root,map,max_val);

if(max_val == 0)
return result;

for(auto it=map.begin();it!=map.end();it++)
{
if(it->second == max_val)
result.push_back(it->first);
}

return result;

}
};
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.