-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBTfromPreAndInorder.java
More file actions
64 lines (52 loc) · 1.63 KB
/
BTfromPreAndInorder.java
File metadata and controls
64 lines (52 loc) · 1.63 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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class BTfromPreAndInorder {
public TreeNode buildTree(int[] preorder, int[] inorder) {
return construct(preorder, inorder);
}
TreeNode construct(int[] preorder, int[] inorder){
if(preorder.length == 0 || inorder.length == 0){
return null;
}
TreeNode root = new TreeNode(preorder[0]);
int rootInd = 0;
for(int i = 0; i<inorder.length; i++){
if(inorder[i] == root.val){
rootInd = i;
break;
}
}
int[] leftInorder = new int[rootInd];
int[] rightInorder = new int[inorder.length - rootInd - 1];
for(int i = 0; i<rootInd; i++){
leftInorder[i] = inorder[i];
}
for(int i = 0; i<rightInorder.length; i++){
rightInorder[i] = inorder[i+rootInd + 1];
}
int[] leftPre = new int[rootInd];
int[] rightPre = new int[preorder.length - rootInd - 1];
for(int i = 0; i<rootInd; i++){
leftPre[i] = preorder[i+1];
}
for(int i = 0; i<rightPre.length; i++){
rightPre[i] = preorder[i+rootInd + 1];
}
root.left = construct(leftPre, leftInorder);
root.right = construct(rightPre, rightInorder);
return root;
}
}