I don't understand why is TLE in this correct code


#1
>     int search(vector<int>&B,int i,int j,int x){
>         for(int init = i ;init<=j ; i++){
>             if(B[init] == x)
>                 return init;
>         }
>         return -1;
>     }
>     TreeNode* construct(vector<int> &A, vector<int> &B,int i,int j){
>         static int indx = 0;
>         if(i>j)
>             return NULL;
>             
>         TreeNode* root  = new TreeNode(A[indx++]);
>         if(i == j)
>             return root;
>             
>         int k = search(B,i,j,root->val);
>         root->left = construct(A,B,i,k-1);
>         root->right = construct(A,B,k+1,j);
>         return root;
>     }
>     TreeNode* Solution::buildTree(vector<int> &A, vector<int> &B) {
>         if(A.empty()||B.empty())
>             return NULL;
>         return construct(A,B,0,A.size()-1);
>     }

#2

Inside search function, in the for loop you put i++ change it to init++


#3

Just swap the order of Two base cases.
first keep i==j and then check for I>j