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

## Subject

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

Note:
You may assume that duplicates do not exist in the tree.

## Solution

### solution 1

     4
/   \
2     7
/ \   / \
1   3 6   9

【2】是【1 3】的父结点。
【1】是 空的父结点，也即使叶子结点。

……

public TreeNode buildTree(int[] preorder, int[] inorder) {
if (preorder == null || preorder.length == 0) {
return null;
}
if (inorder == null || inorder.length == 0) {
return null;
}

if (preorder.length != inorder.length) {
return null;
}

return build(preorder, inorder, 0, 0, inorder.length - 1);
}
public TreeNode build(int[] preorder, int[] inorder, int preIndex,
int startInIndex, int endInIndex) {
if (endInIndex < startInIndex) {
return null;
}
TreeNode node = new TreeNode(preorder[preIndex]);

// the index of current node in inorder
int index = getIndexInInorder(inorder, preorder[preIndex]);
int lenL = index - startInIndex;
int lenR = endInIndex - startInIndex - lenL;

if (lenL > 0) {
node.left = build(preorder, inorder, preIndex + 1, startInIndex,
index - 1);
}
if (lenR > 0) {
node.right = build(preorder, inorder, preIndex + lenL + 1,
index + 1, endInIndex);
}

return node;
}
//获得val值在inorder中的下标
public int getIndexInInorder(int[] inorder, int val) {
for (int i = 0; i < inorder.length; i++) {
if (val == inorder[i]) {
return i;
}
}
return -1;
}

build( ) 是主要递归方法。

LeetCode平台测试通过之后，发现build方法可以稍微优化一些。如下：

public TreeNode build2(int[] preorder, int[] inorder, int preIndex,
int startInIndex, int endInIndex) {
if (endInIndex < startInIndex) {
return null;
}
int currentVal = preorder[preIndex];
TreeNode node = new TreeNode(currentVal);

// the index of current node in inorder
int index;
for (index = startInIndex; index <= endInIndex; index++) {
if (currentVal == inorder[index]) {
break;
}
}
// int lenL = index - startInIndex;
// int lenR = endInIndex - index;

if (index > startInIndex) {
node.left = build(preorder, inorder, preIndex + 1, startInIndex,
index - 1);
}
if (endInIndex > index) {
node.right = build(preorder, inorder, preIndex + index
- startInIndex + 1, index + 1, endInIndex);
}

return node;
}

beats 73.81% of java submissions.

### solution 2

    /**
*
* @param preorder
* @param inorder
* @return
*/
public TreeNode buildTree2(int[] preorder, int[] inorder) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < inorder.length; i++) {
map.put(inorder[i], i);
}
TreeNode root = null;
TreeNode p = root;
Stack<TreeNode> stack = new Stack<>();
for (int i = 0; i < preorder.length; i++) {
int temp = map.get(preorder[i]);
TreeNode node = new TreeNode(preorder[i]);
if (stack.isEmpty()) {
root = node;
p = root;
} else {
if (temp < map.get(stack.peek().val)) {
p.left = node;
p = p.left;
} else {
while (!stack.isEmpty() && temp > map.get(stack.peek().val)) {
p = stack.pop();
}
p.right = node;
p = p.right;
}
}
}

return root;
}

beats 73.29% of java submissions.

bingo~~

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

2016-02-17 09:18:24

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

2014-04-24 08:43:09

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

2015-11-30 23:54:21

#### LeetCode 105：Construct Binary Tree from Preorder and Inorder Traversal

2015-05-20 23:09:41

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

2014-05-21 05:59:08

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

2015-08-09 06:37:17

#### LeetCode(105) Construct Binary Tree from Preorder and Inorder Traversal解题报告

2015-12-12 22:39:59

#### 105. Construct Binary Tree from Preorder and Inorder Traversal，前序+中序 构建 树

2016-12-07 21:00:02

#### [JAVA]LeetCode105 Construct Binary Tree from Preorder and Inorder Traversal

2015-03-29 10:39:08

#### leetCode练习（105）

2016-10-29 14:55:58