# Time Limit Exceeded

• Hi,

Can someone please explain why I am getting TLE for my implementation. Here is the code which I wrote..

``````TreeNode* Solution::buildTree(vector<int>& preorder, vector<int>& inorder) {
TreeNode *root=NULL;
TreeNode *traverse=NULL;
map<int,int> position;
pair<int,int > temp;
for(unsigned int i=0;i<inorder.size();i++){
temp.first=inorder[i];
temp.second=i;
position.insert(temp);
}
for(unsigned int i=0;i<preorder.size();i++,traverse=root){
TreeNode *node=new TreeNode(preorder[i]);
map<int,int>::iterator it2 = position.find(preorder[i]);
int index=it2->second;
if(root!=NULL){
for(;traverse!=NULL;){
if(index<position[traverse->val]){
if(traverse->left!=NULL){
traverse=traverse->left;
}else{
traverse->left=node;
break;
}
}else{
if(traverse->right!=NULL){
traverse=traverse->right;
}else{
traverse->right=node;
break;
}
}
}
}else{
root=node;
traverse=root;
}
}
return root;
}``````

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