forked from xiaoningning/java-algorithm-2010
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildBT.java
More file actions
57 lines (51 loc) · 1.79 KB
/
BuildBT.java
File metadata and controls
57 lines (51 loc) · 1.79 KB
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
import java.util.Arrays;
import java.util.HashMap;
/**
* Given preorder and inorder traversal of a tree, construct the binary tree.
* in-order: left, root, right
* pre-order: root, left, right
* <p/>
* <p/>
* ________ 7______
* / \
* __10__ ___2
* / \ /
* 4 3 _8
* \ /
* 1 11
* preorder = {7,10,4,3,1,2,8,11}
* inorder = {4,10,3,1,7,11,8,2}
*/
public class BuildBT {
public static void main(String[] args) {
int[] preorder = new int[]{7, 10, 4, 3, 1, 2, 8, 11};
int[] inorder = new int[]{4, 10, 3, 1, 7, 11, 8, 2};
// build b tree
Node bt = buildBTInorderPreorder(inorder, preorder);
bt.printBT();
}
public static HashMap<Integer, Integer> mapIndex(int[] inorder) {
HashMap<Integer, Integer> index = new HashMap<Integer, Integer>();
for (int i = 0; i < inorder.length; i++) {
index.put(inorder[i], i);
}
return index;
}
public static Node buildBTInorderPreorder(int[] in,
int[] pre) {
if (in.length == 0 || pre.length != in.length)
return null;
// get index of element of inOrder
HashMap<Integer, Integer> index = new HashMap<Integer, Integer>();
for (int i = 0; i < in.length; i++) {
index.put(in[i], i);
}
Node root = new Node(pre[0]);
int rootIndex = index.get(pre[0]);
root.left = buildBTInorderPreorder(Arrays.copyOfRange(in, 0, rootIndex),
Arrays.copyOfRange(pre, 1, rootIndex+1));
root.right = buildBTInorderPreorder(Arrays.copyOfRange(in, rootIndex + 1, in.length),
Arrays.copyOfRange(pre, rootIndex+1, pre.length));
return root;
}
}