Can Someone explain for me why?


  • 0
    Z
    class Solution {
    public:
        vector<int> findFrequentTreeSum(TreeNode* root) {
    		unordered_map<int,int> frequentMap;
    		int maxFrequent=0;
    		vector<int> ans;
    		searchChild(root,frequentMap,maxFrequent);
    		for (auto frequent:frequentMap)
    			if(frequent.second==maxFrequent)
    				ans.push_back(frequent.first);
    		return ans;
        }
    	int searchChild(TreeNode* root,unordered_map<int,int>& frequentMap,int& maxFrequent){
    		if(root==NULL)
    			return 0;
    		else{
    			int left=0,right=0,sum=0;
    			if(root->left)
    				left=searchChild(root->left,frequentMap,maxFrequent);
    			if(root->right)
    				right=searchChild(root->right,frequentMap,maxFrequent);
    			sum=root->val+left+right;
    			frequentMap[sum]++;
    			maxFrequent=max(maxFrequent,frequentMap[sum]);
    			return sum;
    		}
    	}
    };
    

    I run this code in VS2012 and I can get the result,but when I use leetcode to test my code, it return []。can someone help me to explain why?


Log in to reply
 

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