-
Notifications
You must be signed in to change notification settings - Fork 0
/
PreorderToBST.java
33 lines (30 loc) · 963 Bytes
/
PreorderToBST.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
package TreeQuestion_gfg;
// GFG : https://www.geeksforgeeks.org/problems/preorder-to-postorder4423/1
// Question : Preorder to BST
public class PreorderToBST {
public static Node constructBST(int[] preorder){
return bst(preorder,Integer.MAX_VALUE,new int[]{0});
}
public static Node bst(int[] pre,int bound,int[] a){
if(a[0]==pre.length || pre[a[0]]> bound){
return null;
}
Node root= new Node(pre[a[0]++]);
root.left=bst(pre,root.data,a);
root.right=bst(pre,bound,a);
return root;
}
public static void printPreOrder(Node node) {
if (node == null) {
return;
}
System.out.print(node.data + " ");
printPreOrder(node.left);
printPreOrder(node.right);
}
public static void main(String[] args) {
int[] arr = {40, 30, 35, 80, 100};
Node root = constructBST(arr);
printPreOrder(root);
}
}