Getting wrong answer


  • 0
    I

    I am getting wrong ans in the following code, please can somebody help me correct it or post the correct the code. Thank you

    void recoverTree(TreeNode *root) {
        static TreeNode* temp1,*temp2,*prev;
    	static int found;
    	if(root)
    	{
    		recoverTree(root->left);
    		if(!temp1 && prev && root->val < prev->val)
            {
    			temp1=prev;
                temp2=root;
            }
    		else if(prev && root->val < prev->val)
    			temp2=root;
    		if(temp1 && temp2 && !found)
    		{
    			swap(&(temp1->val),&(temp2->val));
    			found=1;
    			return;
    		}
    		prev=root;
    		recoverTree(root->right);
    	}
    }
    
    void swap(int *a, int *b){
        int temp = *a;
        *a = *b;
        *b = temp;
    }

  • 0
    I
    1. you assigned root to temp2 when temp1 is found, then end the function because (temp1 && temp2 && !found)......if the 2 nodes are not neighbor, the method will fail
    2. is static variable ok without reset every time ?

Log in to reply
 

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