C++ Map somehow need to simplify


  • 0
    class Solution {
        int m=1;//INT_MIN; at lease 1
        unordered_map<int, int> map;//node val, frequency
        vector<int> result;
    public:
        int find(TreeNode* root, int sum)
        {
            if(!root) return sum;
            sum += root->val +find(root->left, 0) + find(root->right, 0);
            if(map.find(sum)!=map.end())
                m=max(++map[sum],m);
            else
                map[sum]=1;
            return sum;
        }
        vector<int> findFrequentTreeSum(TreeNode* root) {
            if(!root)  return result;
            find(root,0);
            for(auto fm : map)
                if(fm.second == m)
                    result.push_back(fm.first);
            return result;
        }
    };
    

Log in to reply
 

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