Leetcode show wrong answer,but is right when i test in my computer


  • 0
    Z

    class Solution {

    public:
    
        vector<string> binaryTreePaths(TreeNode* root) {
        static vector<string> vPath;
        string s;
        if(root!=NULL)
        	fun(vPath,root,s);
        return vPath;
    }
    
    void fun(vector<string> &path,TreeNode *root, string temp){
    	
    	if(root==NULL)	
    		return ;
    	
    	
    	char trans[10];
    	sprintf(trans,"%d",root->val); 
    	temp+=trans;
    	
    	
    	if(root->left==NULL && root->right==NULL){
    		path.push_back(temp);
    		
    	}
    	
    	if(root->left!=NULL){
    		temp+="->";
    		fun(path,root->left,temp);
    	}
    	if(root->right!=NULL){
    		if(root->left==NULL)
    			temp+="->";
    		fun(path,root->right,temp);
    	}
    
    }
    

    };


  • 1

    Please read item no. 3 in the FAQ, as all test cases were run in the same process, you will have to reset your static/global variables.


  • 0
    Z

    Thank you very much, you really helped me !


Log in to reply
 

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