# Construct Binary Tree from Inorder and Postorder Traversal

/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
if (inorder.length != postorder.length) {
return null;
}
return myBuildTree(inorder, 0, inorder.length - 1, postorder, 0, postorder.length - 1);
}
private TreeNode myBuildTree(int[] inorder, int inBeg, int inEnd, int[] postorder, int postBeg, int postEnd) {
if (inBeg > inEnd) {
return null;
}
TreeNode root = new TreeNode(postorder[postEnd]);
int location = findLocation(inorder, inBeg, inEnd, postorder[postEnd]);
root.left = myBuildTree(inorder, inBeg, location - 1, postorder, postBeg, postBeg + location - inBeg - 1);
root.right = myBuildTree(inorder, location + 1, inEnd, postorder, postEnd + location - inEnd, postEnd - 1);
return root;
}
private int findLocation(int[] inorder, int begin, int end, int value) {
for (int i = begin; i <= end; i++) {
if (inorder[i] == value) {
return i;
}
}
return -1;
}
}

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

2014-04-24 09:02:17

#### 106 Construct Binary Tree from Inorder and Postorder Traversal

2015-12-09 20:29:35

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

2015-08-09 06:39:31

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

2013-11-12 13:20:50

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

2014-04-24 08:43:09

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

2014-12-04 23:07:11

#### LeetCode: Construct Binary Tree from Inorder and Postorder Traversal [106]

2014-06-03 08:23:27

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

2016-06-01 23:42:08

#### LeetCode 106：Construct Binary Tree from Postorder and Inorder Traversal

2015-06-01 19:09:42

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

2014-05-21 05:59:08