This code passed on my local machine but not here


  • 0
    E

    This code can passed on my local computer but it got run time error on here.
    This wrong case is :
    Input: [1,2] [1,2]
    Runtime error

    struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    

    };
    int pre_index=0;
    struct TreeNode *buildTreeR(int preorder, int inorder, int n,int in_s , int in_e) {
    int i,in_index=0;
    if(in_s > in_e) return NULL;
    struct TreeNode
    root = (struct TreeNode )malloc(sizeof (struct TreeNode));
    root->val=
    (preorder+pre_index++);
    printf("root->val = %d in_s=%d in_e=%d\n",root->val,in_s,in_e);
    if(in_s == in_e) return root;
    for(i = in_s; i <= in_e; i++){
    if(
    (inorder+i) == root->val){
    printf("root->val = %d in_index=%d in_s=%d in_e=%d\n",root->val,in_index,in_s,in_e);
    in_index=i;break;
    }
    }
    root->left=buildTreeR(preorder,inorder,n,in_s,in_index-1);
    root->right=buildTreeR(preorder,inorder,n,in_index+1,in_e);
    return root;
    }
    struct TreeNode *buildTree(int *preorder, int *inorder, int n) {
    if(n == 0) return NULL;
    return buildTreeR(preorder,inorder,n,0,n-1);
    }


Log in to reply
 

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