Skip to content

Commit c4786c8

Browse files
committed
ConstructBinaryTreeFromInorderAndPostorderTraversal106
1 parent 3d5b981 commit c4786c8

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

Diff for: README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@
5353
| [Graph](https://github.com/fluency03/leetcode-java/blob/master/src/graph) |
5454

5555

56-
# Total: 467
56+
# Total: 468
5757

5858
| Easy | Medium | Hard | - |
5959
|:-------:|:-------:|:----:|:-:|
60-
| 126 | 262 | 74 | 5 |
60+
| 126 | 263 | 74 | 5 |
6161

6262

6363
| Question | Solution | Difficulty |
@@ -150,6 +150,7 @@
150150
| [103. Binary Tree Zigzag Level Order Traversal](https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/BinaryTreeZigzagLevelOrderTraversal103.java) | Medium |
151151
| [104. Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/MaximumDepthOfBinaryTree104.java) | Easy |
152152
| [105. Construct Binary Tree from Preorder and Inorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/ConstructBinaryTreeFromPreorderAndInorderTraversal105.java) | Medium |
153+
| [106. Construct Binary Tree from Inorder and Postorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/ConstructBinaryTreeFromInorderAndPostorderTraversal106.java) | Medium |
153154
| [107. Binary Tree Level Order Traversal II](https://leetcode.com/problems/binary-tree-level-order-traversal-ii/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/BinaryTreeLevelOrderTraversalII107.java) | Easy |
154155
| [108. Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/ConvertSortedArrayToBinarySearchTree108.java) | Easy |
155156
| [109. Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/ConvertSortedListToBST109.java) | Medium |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Given inorder and postorder traversal of a tree, construct the binary tree.
3+
*
4+
* Note:
5+
* You may assume that duplicates do not exist in the tree.
6+
*
7+
* For example, given
8+
*
9+
* inorder = [9,3,15,20,7]
10+
* postorder = [9,15,7,20,3]
11+
* Return the following binary tree:
12+
*
13+
* 3
14+
* / \
15+
* 9 20
16+
* / \
17+
* 15 7
18+
*/
19+
20+
/**
21+
* Definition for a binary tree node.
22+
* public class TreeNode {
23+
* int val;
24+
* TreeNode left;
25+
* TreeNode right;
26+
* TreeNode(int x) { val = x; }
27+
* }
28+
*/
29+
30+
public class ConstructBinaryTreeFromInorderAndPostorderTraversal106 {
31+
public TreeNode buildTree(int[] inorder, int[] postorder) {
32+
if (inorder == null || postorder == null || inorder.length == 0 || postorder.length == 0 || inorder.length != postorder.length) return null;
33+
int len = inorder.length;
34+
return buildTree(inorder, 0, len-1, postorder, 0, len-1);
35+
}
36+
37+
private TreeNode buildTree(int[] inorder, int ii, int ij, int[] postorder, int pi, int pj) {
38+
if (ii > ij) return null;
39+
int midVal = postorder[pj];
40+
TreeNode curr = new TreeNode(midVal);
41+
if (pi == pj) return curr;
42+
43+
int mid = ii;
44+
while (inorder[mid] != midVal) mid++;
45+
46+
curr.left = buildTree(inorder, ii, mid-1, postorder, pi, pi+(mid-1-ii));
47+
curr.right = buildTree(inorder, mid+1, ij, postorder, pi+(mid-ii), pj-1);
48+
return curr;
49+
}
50+
51+
}

0 commit comments

Comments
 (0)