-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBTfromInAndPostOrder.java
More file actions
44 lines (34 loc) · 904 Bytes
/
BTfromInAndPostOrder.java
File metadata and controls
44 lines (34 loc) · 904 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package tree;
public class BTfromInAndPostOrder {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public TreeNode construct(int[] inorder, int[] postorder, int is, int ie, int ps, int pe) {
if (ps > pe) {
return null;
}
TreeNode rv = new TreeNode(postorder[pe]);
int index = -1;
for (int i = is; i <= ie; ++i) {
if (inorder[i] == postorder[pe]) {
index = i;
break;
}
}
int num = index - is - 1;
rv.left = construct(inorder, postorder, is, index - 1, ps, ps + num);
rv.right = construct(inorder, postorder, index + 1, ie, ps + num + 1, pe - 1);
return rv;
}
public TreeNode buildTree(int[] inorder, int[] postorder) {
return construct(inorder, postorder, 0, inorder.length - 1, 0, postorder.length - 1);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}