Skip to content

Commit dbec940

Browse files
committed
feat(solution): add solution 0106 [Java]
Construct Binary Tree from Inorder and Postorder Traversal
1 parent bae9138 commit dbec940

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ Complete [solutions](https://github.com/doocs/leetcode/tree/master/solution) to
8888
| 0096 | [Unique Binary Search Trees](https://github.com/doocs/leetcode/tree/master/solution/0096.Unique%20Binary%20Search%20Trees) | `Tree`, `Dynamic Programming` |
8989
| 0102 | [Binary Tree Level Order Traversal](https://github.com/doocs/leetcode/tree/master/solution/0102.Binary%20Tree%20Level%20Order%20Traversal) | `Tree`, `Breadth-first Search` |
9090
| 0105 | [Construct Binary Tree from Preorder and Inorder Traversal](https://github.com/doocs/leetcode/tree/master/solution/0105.Construct%20Binary%20Tree%20from%20Preorder%20and%20Inorder%20Traversal) | `Array`, `Tree`, `Depth-first Search` |
91+
| 0106 | [Construct Binary Tree from Inorder and Postorder Traversal](https://github.com/doocs/leetcode/tree/master/solution/0106.Construct%20Binary%20Tree%20from%20Inorder%20and%20Postorder%20Traversal) | `Array`, `Tree`, `Depth-first Search` |
9192
| 0127 | [Word Ladder](https://github.com/doocs/leetcode/tree/master/solution/0127.Word%20Ladder) | `Breadth-first Search` |
9293
| 0130 | [Surrounded Regions](https://github.com/doocs/leetcode/tree/master/solution/0130.Surrounded%20Regions) | `Depth-first Search`, `Breadth-first Search`, `Union Find` |
9394
| 0137 | [Single Number II](https://github.com/doocs/leetcode/tree/master/solution/0137.Single%20Number%20II) | `Bit Manipulation` |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
## 从中序与后序遍历序列构造二叉树
2+
3+
### 问题描述
4+
根据一棵树的中序遍历与后序遍历构造二叉树。
5+
6+
**注意:**
7+
8+
你可以假设树中没有重复的元素。
9+
10+
例如,给出
11+
```
12+
中序遍历 inorder = [9,3,15,20,7]
13+
后序遍历 postorder = [9,15,7,20,3]
14+
```
15+
16+
返回如下的二叉树:
17+
```
18+
3
19+
/ \
20+
9 20
21+
/ \
22+
15 7
23+
```
24+
25+
### 解法
26+
27+
利用树的后序遍历和中序遍历特性 + 递归实现。
28+
29+
树的后序遍历序列,从后往前,对于每一个元素,在树的中序遍历中找到该元素;在中序遍历中,该元素的左边是它的左子树的全部元素,右边是它的右子树的全部元素,以此为递归条件,确定左右子树的范围。
30+
31+
```java
32+
/**
33+
* Definition for a binary tree node.
34+
* public class TreeNode {
35+
* int val;
36+
* TreeNode left;
37+
* TreeNode right;
38+
* TreeNode(int x) { val = x; }
39+
* }
40+
*/
41+
class Solution {
42+
public TreeNode buildTree(int[] inorder, int[] postorder) {
43+
if (inorder == null || postorder == null || inorder.length != postorder.length) {
44+
return null;
45+
}
46+
int n = inorder.length;
47+
return n > 0 ? buildTree(inorder, 0, n - 1, postorder, 0, n - 1) : null;
48+
}
49+
50+
private TreeNode buildTree(int[] inorder, int s1, int e1, int[] postorder, int s2, int e2) {
51+
TreeNode node = new TreeNode(postorder[e2]);
52+
if (s2 == e2 && s1 == e1) {
53+
return node;
54+
}
55+
56+
int p = s1;
57+
while (inorder[p] != postorder[e2]) {
58+
++p;
59+
if (p > e1) {
60+
throw new IllegalArgumentException("Invalid input!");
61+
}
62+
}
63+
64+
node.left = p > s1 ? buildTree(inorder, s1, p - 1, postorder, s2, p - 1 + s2 - s1) : null;
65+
node.right = p < e1 ? buildTree(inorder, p + 1, e1, postorder, p + s2 - s1, e2 - 1) : null;
66+
return node;
67+
68+
}
69+
}
70+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public TreeNode buildTree(int[] inorder, int[] postorder) {
12+
if (inorder == null || postorder == null || inorder.length != postorder.length) {
13+
return null;
14+
}
15+
int n = inorder.length;
16+
return n > 0 ? buildTree(inorder, 0, n - 1, postorder, 0, n - 1) : null;
17+
}
18+
19+
private TreeNode buildTree(int[] inorder, int s1, int e1, int[] postorder, int s2, int e2) {
20+
TreeNode node = new TreeNode(postorder[e2]);
21+
if (s2 == e2 && s1 == e1) {
22+
return node;
23+
}
24+
25+
int p = s1;
26+
while (inorder[p] != postorder[e2]) {
27+
++p;
28+
if (p > e1) {
29+
throw new IllegalArgumentException("Invalid input!");
30+
}
31+
}
32+
33+
node.left = p > s1 ? buildTree(inorder, s1, p - 1, postorder, s2, p - 1 + s2 - s1) : null;
34+
node.right = p < e1 ? buildTree(inorder, p + 1, e1, postorder, p + s2 - s1, e2 - 1) : null;
35+
return node;
36+
37+
}
38+
}

0 commit comments

Comments
 (0)