Submission fails, but locally it produce expected o/p


  • 0
    A

    Hi,

    Following is my code, on input [0] my submission is failing, saying that expected output is 1 and my output is 0, however when I run the test case locally my code produce 1 as output, then again I tried submitting and this time it failed with output [1,2] saying expected o/p 2 and my o/p 1, so I run locally with 1 in root and 2 in left and found o/p 2. Following is my code,

    unsigned int minVal = 0xffffffff;
    class Solution {
        private:
        void getDepth(TreeNode *root, int d)
          {
            if(root == NULL)
            {
                minVal = 0;
                return;
            }
            if(root->left==NULL && root->right==NULL)
            {
                if(d<minVal)
                    minVal = d;
                return;
            }
    		if(root->left)
    			getDepth(root->left,d+1);
    		if(root->right)
    			getDepth(root->right,d+1);
        }
    public:
        int minDepth(TreeNode *root) {
    		if(root == NULL)
    			return 0;
            getDepth(root,1);
            return minVal;
        }
    };
    

    Regards,

    Arka


  • 0

    As all test cases are run in the same process, you have to reset your minVal variable. Please read the FAQ, especially the question: "Why does my code produce a different output compared to my local environment?"


  • 0
    A

    Thanks a lot for your help. I should have considered the fact that all the test cases are run like that.


Log in to reply
 

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