-
Notifications
You must be signed in to change notification settings - Fork 1
/
leetcode105.java
63 lines (54 loc) · 1.68 KB
/
leetcode105.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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
int lengthOfPre=preorder.length;
int lengthOfIn=inorder.length;
if(lengthOfPre==0){
return null;
}
if(lengthOfPre==1){
TreeNode root=new TreeNode(preorder[0]);
root.left=root.right=null;
return root;
}
TreeNode root=buildBinaryTree(preorder,0,lengthOfPre-1,inorder,0,lengthOfIn-1);
return root;
}
public TreeNode buildBinaryTree(int[] preorder,int startOfPre,int endOfPre,int[] inorder,int startOfIn,int endOfIn){
if(startOfPre==endOfPre){
TreeNode root=new TreeNode(preorder[startOfPre]);
//root.val=preorder[startOfPre];
root.left=null;
root.right=null;
return root;
}
int i=0;
for(i=startOfIn;i<=endOfIn;i++){
if(inorder[i]==preorder[startOfPre]){
break;
}
}
TreeNode root=new TreeNode(preorder[startOfPre]);
if(i-1>=startOfIn){
root.left=buildBinaryTree(preorder,startOfPre+1,startOfPre+i-startOfIn,inorder,startOfIn,i-1);
//buildBinaryTree(preorder,startOfPre+1,startOfPre+i-startOfIn-1,inorder,startOfIn,i-1);
}
else
root.left=null;
if(i!=endOfIn){
root.right=buildBinaryTree(preorder,startOfPre+i-startOfIn+1,endOfPre,inorder,i+1,endOfIn);
//buildBinaryTree(preorder,startOfPre+i-startOfIn,endOfPre,inorder,i+1,endOfIn);
}
else
root.right=null;
return root;
}
}