-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathBinaryTreeFromInorderPostOrder.java
More file actions
53 lines (39 loc) · 1.15 KB
/
BinaryTreeFromInorderPostOrder.java
File metadata and controls
53 lines (39 loc) · 1.15 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
package Trees;
import java.util.ArrayList;
/**
* Author - archit.s
* Date - 05/11/18
* Time - 4:36 PM
*/
public class BinaryTreeFromInorderPostOrder {
class Index{
int index;
public Index(int index){
this.index = index;
}
}
public int search(ArrayList<Integer> A, int value, int start, int end){
for(int i=start;i<=end;i++){
if(A.get(i) == value){
return i;
}
}
return -1;
}
public TreeNode helper(ArrayList<Integer> inorder, ArrayList<Integer> postorder,
Index idx, int start, int end){
if(start > end){
return null;
}
int t = search(inorder, postorder.get(idx.index), start, end);
idx.index-=1;
TreeNode root = new TreeNode(inorder.get(t));
root.right = helper(inorder,postorder, idx,t+1,end);
root.left = helper(inorder,postorder, idx,start,t-1);
return root;
}
public TreeNode buildTree(ArrayList<Integer> A, ArrayList<Integer> B) {
Index i = new Index(A.size()-1);
return helper(A,B,i,0,A.size()-1);
}
}