# Getting Time limit Exceeded - Why?

• I am getting time limit exceeded for my code, though it looks simple. Can someone help me in finding the root cause?

``````class Solution {
public:
TreeNode *buildTree1(vector<int> &preorder, vector<int> &inorder, int pb,int pe,int ib,int ie,int psiz, int isiz)
{
int pos;

if(pb>=psiz || pe>=psiz || ib>=preorder.size() || ie>=isiz)
return NULL;
TreeNode *root;
root=new TreeNode(preorder[pb]);
if(pb==pe || ib==ie)
return root;
for(int i=ib;i<=ie;i++)
{
if(inorder[i]==preorder[pb])
{
pos=i;
break;
}
}
root->left = buildTree1(preorder,inorder,pb+1,pb+pos,ib,ib+pos,psiz,isiz);
root->right = buildTree1(preorder,inorder,pb+pos+1,pe,ib+pos+1,ie,psiz,isiz);
return root;

}
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
int pos,i;
TreeNode *root;
if(preorder.empty() || inorder.empty())
return NULL;
root = buildTree1(preorder,inorder,0,preorder.size()-1,0,inorder.size()-1,preorder.size(),inorder.size());
return root;
}
``````

};

• Same problem here....

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