-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
79 lines (67 loc) · 1.94 KB
/
Solution.java
File metadata and controls
79 lines (67 loc) · 1.94 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package binarytree;
public class Solution {
public TreeNode reConstructBinaryTree(int[] pre, int[] in) {
if ( pre == null || in == null || pre.length != in.length ) {
throw new RuntimeException("reConstructBinaryTree输入错误");
}
int rootValue = pre[0];
TreeNode root = new TreeNode( rootValue );
if ( pre.length == 1 ) {
if ( in.length == 1 && rootValue == in[0] ) {
return root;
} else {
throw new RuntimeException("reConstructBinaryTree输入错误");
}
}
// rootIndex int[] in中rootValue对应的下标
int rootIndex = 0;
while ( rootIndex <= in.length -1 && rootValue != in[rootIndex] ) {
rootIndex++ ;
}
if ( rootIndex >= in.length || rootValue != in[rootIndex]) {
throw new RuntimeException("reConstructBinaryTree输入错误");
}
// 递归生成左子树
if ( rootIndex > 0 ) {
int[] subPre = getSubTree(pre,1,rootIndex);
int[] subIn = getSubTree(in,0,rootIndex);
root.left = reConstructBinaryTree( subPre, subIn );
}
// 递归生成右子树
int count = in.length - rootIndex - 1;
if ( count > 0 ) {
int[] subPre = getSubTree(pre,rootIndex+1,count);
int[] subIn = getSubTree(in,rootIndex+1,count);
root.right = reConstructBinaryTree( subPre, subIn );
}
return root;
}
/**
* getSubTree TODO :
* @param srcArr
* @param begin
* @param count
* @return
* @author zhiman
* @date 2018/01/25 上午10:21:22
*/
private int[] getSubTree(int[] srcArr, int begin, int count) {
int[] dstArr = new int[count];
System.arraycopy(srcArr, begin, dstArr, 0, count);
return dstArr;
}
public static void main(String[] args) {
int[] pre = {1,2,4,7,3,5,6,8};
int[] in = {4,7,2,1,5,3,8,6};
TreeNode root = new Solution().reConstructBinaryTree(pre, in);
viewBinTree(root);
}
public static void viewBinTree( TreeNode root ) {
if ( root != null) {
//System.out.println(root.val);
viewBinTree(root.left);
System.out.println(root.val);
viewBinTree(root.right);
}
}
}