# [leetcode]105. Construct Binary Tree from Preorder and Inorder Traversal

/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
int len=preorder.size();
if(len==0)
return {};
else
return build(preorder,inorder,0,len-1,0,len-1);
}
TreeNode* build(vector<int>& preorder, vector<int>& inorder,int preL,int preR,int inL,int inR)
{
if(preR<preL||inR<inL)
return {};

TreeNode* root=new TreeNode(preorder[preL]);
if(preR==preL||inR==inL)
return root;

int mid;
for(int i=inL;i<=inR;i++)
{
if(inorder[i]==preorder[preL])
{
mid=i;
break;
}
}
root->left=build(preorder,inorder,preL+1,preL+mid-inL,inL,mid-1);
root->right=build(preorder,inorder,preL+1+mid-inL,preR,mid+1,inR);
return root;

}
};



#### 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 解题报告

2016-06-01 23:42:08

#### 【LeetCode-面试算法经典-Java实现】【105-Construct Binary Tree from Preorder and Inorder Traversal（构造二叉树）】

2015-08-09 06:37:17

#### C++详解Leetcode:105. Construct Binary Tree from Preorder and Inorder Traversal

2017-07-30 17:34:05

#### [LeetCode-21]Construct Binary Tree from Preorder and Inorder Traversal

2014-05-21 05:59:08

#### Construct Binary Tree from Preorder and Inorder Traversal -- LeetCode

2014-04-24 08:43:09

#### 【LeetCode-面试算法经典-Java实现】【106-Construct Binary Tree from Inorder and Postorder Traversal（构造二叉树II）】

2015-08-09 06:39:31

#### leetcode笔记：Construct Binary Tree from Preorder and Inorder Traversal

2015-11-30 23:54:21

#### LeetCode Construct Binary Tree from Inorder and Postorder Traversal 思考分析过程分享

2013-11-12 13:20:50

#### Given preorder and inorder traversal of a tree, construct the binary tree.

2014-12-04 23:07:11

#### Construct Binary Tree from Inorder and Postorder Traversal -- LeetCode

2014-04-24 09:02:17