-
Notifications
You must be signed in to change notification settings - Fork 2
/
ConstructBinTreeFromPostorderAndInorder.java
73 lines (45 loc) · 1.21 KB
/
ConstructBinTreeFromPostorderAndInorder.java
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package chapter3;
public class ConstructBinTreeFromPostorderAndInorder {
public static int idx = 1;
public static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public static void solution(int[] postArray, int[] inArray, int start, int end, TreeNode p) {
if (start <= end) {
TreeNode np = new TreeNode(postArray[idx]);
int pos = getPos(inArray, np.val, start, end);
idx--;
if (p.val > postArray[idx]) {
// left
p.left = np;
p = np;
solution(postArray, inArray, start, pos - 1, p);
} else {
p.right = np;
p = np;
solution(postArray, inArray, pos + 1, end, p);
}
}
}
public static int getPos(int[] inArray, int value, int start, int end) {
int mid = (start + end) / 2;
if (value == inArray[mid])
return mid;
else if (value > inArray[mid])
return getPos(inArray, value, mid + 1, end);
else
return getPos(inArray, value, start, mid - 1);
}
public static void main(String[] args) {
int[] inArray = new int[8];
int[] postArray = new int[8];
idx = postArray.length -2;
TreeNode root = new TreeNode(postArray[idx+1]);
solution(postArray,inArray,0,inArray.length,root);
}
}