-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
Description
/**
* 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 {
public:
unordered_map<int,int> m;
int sumSubTree(TreeNode* root){
if(root == nullptr) return 0;
int left = sumSubTree(root->left);
int right = sumSubTree(root->right);
m[left + right + root->val] += 1;
return left + right + root->val;
}
vector<int> findFrequentTreeSum(TreeNode* root) {
sumSubTree(root);
int MAX = 0;
int ket = -1;
vector<int> ret;
for(auto p : m ){
if(p.second > MAX){
MAX = p.second;
ret = vector<int>();
ret.push_back(p.first);
} else if(p.second == MAX){
ret.push_back(p.first);
}
}
return ret;
}
};