-
Notifications
You must be signed in to change notification settings - Fork 1
/
Q7_RebuildBinaryTreeThroughPreAndIn.java
69 lines (62 loc) · 2.07 KB
/
Q7_RebuildBinaryTreeThroughPreAndIn.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
package jianzhi_offer;
/**
* LeetCode 105
*
* @author weiyuwang
* @since 2019/1/23 10:24
*/
public class Q7_RebuildBinaryTreeThroughPreAndIn {
static class TreeNode{
int val;
TreeNode left;
TreeNode right;
public TreeNode(int val) {
this.val = val;
}
}
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};
buildTree(pre,in);
}
/**
* 根据前序和中序建立二叉树
* @param preorder
* @param inorder
* @return
*/
private static TreeNode buildTree(int[] preorder, int[] inorder) {
if(preorder == null || inorder == null || preorder.length != inorder.length || preorder.length < 1){
return null;
}
return build(preorder,inorder,0,preorder.length-1,0,inorder.length-1);
}
private static TreeNode build(int[] preorder,int[] inorder,int prestart,int preend,int instart,int inend){
// System.out.println(""+prestart + preend + instart + inend);
if(prestart > preend || instart > inend){
return null;
}else if(prestart == preend){
return new TreeNode(preorder[prestart]);
}else{
TreeNode root = new TreeNode(preorder[prestart]);
int rootIndex = find(preorder[prestart],inorder,instart,inend);
if(rootIndex == -1){
return null;
}
// System.out.println(""+prestart + preend + instart + inend+rootIndex);
int leftLen = rootIndex - instart;
int leftPreEnd = prestart + leftLen;
root.left = build(preorder,inorder,prestart+1,leftPreEnd,instart,rootIndex-1);
root.right = build(preorder,inorder,leftPreEnd+1,preend,rootIndex+1,inend);
return root;
}
}
private static int find(int val,int[] inorder,int instart,int inend){
for(int i = instart;i <= inend;i++){
if(inorder[i] == val){
return i;
}
}
return -1;
}
}