-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathInorderCartesian.java
More file actions
45 lines (32 loc) · 827 Bytes
/
InorderCartesian.java
File metadata and controls
45 lines (32 loc) · 827 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package Trees;
import java.util.ArrayList;
/**
* Author - archit.s
* Date - 05/11/18
* Time - 1:37 PM
*/
public class InorderCartesian {
//Max Element
public int getRoot(ArrayList<Integer> A, int s, int e){
int ans = s;
for(int i=s;i<=e;i++){
if(A.get(i) > A.get(ans)){
ans = i;
}
}
return ans;
}
public TreeNode build(ArrayList<Integer> A, int start, int end){
if(start > end){
return null;
}
int max = getRoot(A,start,end);
TreeNode root = new TreeNode(A.get(max));
root.left = build(A,start,max-1);
root.right = build(A,max+1,end);
return root;
}
public TreeNode buildTree(ArrayList<Integer> A) {
return build(A,0,A.size()-1);
}
}