给你一个二叉树的根结点,请你找出出现次数最多的子树元素和。一个结点的「子树元素和」定义为以该结点为根的二叉树上所有结点的元素之和(包括结点本身)。
你需要返回出现次数最多的子树元素和。如果有多个元素出现的次数相同,返回所有出现次数最多的子树元素和(不限顺序)。
 
示例 1:
输入:
5/ \2 -3
返回 [2, -3, 4],所有的值均只出现一次,以任意顺序返回所有值。
示例 2:
输入:
5
 /  \
2   -5
返回 [2],只有 2 出现两次,-5 只出现 1 次。
 
提示: 假设任意子树元素和均可以用 32 位有符号整数表示。
/**
 * 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> hashMap;
    vector<int> findFrequentTreeSum(TreeNode* root) {
        treeSum(root);
        unordered_map<int,int>::iterator it;
        int max_freq = 0;
        vector<int> res;
        for(it = hashMap.begin(); it != hashMap.end(); it++){
            // cout<<it->first<<" "<<it->second<<endl;
            if(it->second > max_freq){
                max_freq = it->second;
                res.clear();
                res.push_back(it->first);
            }else if(it->second == max_freq){
                res.push_back(it->first);
            }
        }
        return res;
    }
    int treeSum(TreeNode* root){
        if(root == NULL) return 0;
        if(root->left == NULL && root->right == NULL){
            hashMap[root->val]++;
            return root->val;
        } 
        int left = treeSum(root->left);
        int right = treeSum(root->right);
        cout<<  left + right + root->val<<endl;
        hashMap[left + right + root->val]++;
        return left + right + root->val;
    }
};
                    