-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClass_4.java
More file actions
28 lines (25 loc) · 742 Bytes
/
Class_4.java
File metadata and controls
28 lines (25 loc) · 742 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
package niuke;
public class Class_4 {
public TreeNode reConstructBinaryTree(int[] pre, int[] in) {
return reConstructBinaryTree(pre, in, 0, pre.length-1, 0, pre.length-1);
}
public int findIndex(int[] arr,int a){
int i=0;
while(i<arr.length){
if(a==arr[i]){
return i;
}
else i++;
}
return 0;
}
public TreeNode reConstructBinaryTree(int[] pre, int[] in,int pst,int pend,int ist,int iend) {
if(pst>pend || ist>iend) return null;
int num=pre[pst];
TreeNode tn=new TreeNode(num);
int current=findIndex(in,num);
tn.left=reConstructBinaryTree(pre, in, pst+1, pst+current-ist, ist, current-1);
tn.right=reConstructBinaryTree(pre, in, pst+current-ist+1, pend, current+1, iend);
return tn;
}
}