Runtime error while submitting, which works fine in case of custom input : but fails when I try to submit solution.


  • 0
    C
    struct TreeNode* buildTreeRecursive(int* preorder, int* inorder, int inorderSize) {
        struct TreeNode* root;
        if (inorderSize <= 0) return NULL;
        
        root = (struct TreeNode*) malloc (sizeof(struct TreeNode));
        root->val = preorder[preorderIndex];
        printf("root val = %d, preorderIndex = %d, preorder 1st element = %d, inorderSize = %d\n", root->val, preorderIndex, preorder[0], inorderSize);
    
        preorderIndex++;
        if (inorderSize == 1) return root;
        
        
        //Finding index in inorder.
        int inorderIndex = indexSearch(inorder, inorderSize, root->val);
        printf("inorderIndex = %d\n", inorderIndex);
        
        root->left = buildTreeRecursive(preorder, inorder, inorderIndex);
        root->right = buildTreeRecursive(preorder, inorder + inorderIndex + 1, inorderSize - inorderIndex -1);
        
        return root;    
    }
    

    When I try to submit its shows runtime error for this test case :

    Last executed input:
    [1,2,3]
    [2,1,3]

    It works fine when I run through custom test case. Can someone tell me what am I doing wrong here ?


Log in to reply
 

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