Why it shows TIME LIMIT EXCEED?


#1

int find( vector< int > v, int f, int s, int e )
{
int i;
for( i=s; i<=e; i++ )
{

    if( v[i]==f ) break;
    
}

return i;

}

TreeNode* helper( vector< int > & A, vector< int > & B, int s, int e, int &t )
{

if( t>=B.size() || s>e ) return NULL;

int index=find( B, A[t], s, e );

TreeNode* curr = new TreeNode( A[t] ); t++;

if( s<e )
{
    
    curr->left  = helper( A, B, s, index-1, t  );
    curr->right = helper( A, B, index+1, e, t  );
    
}

return curr;

}

TreeNode* Solution::buildTree(vector &A, vector &B) {

int t=0;
return helper( A, B, 0, B.size()-1, t );

}


#2

You can replace the for loop in the find function with the HASHMAP it will do the same work in O(1)