-
Notifications
You must be signed in to change notification settings - Fork 3
/
Array_To_Bst.java
61 lines (48 loc) · 1010 Bytes
/
Array_To_Bst.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
package com.geek;
import java.util.Scanner;
public class Array_To_Bst {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
Node3 root=getBst(a,0,n-1);
preOrder(root);
}
}
private static Node3 getBst(int[] a, int start, int end) {
if(start>end)
return null;
int mid=(start+end)/2;
Node3 node=new Node3(a[mid]);
node.left=getBst(a, start, mid-1);
node.right=getBst(a, mid+1, end);
return node;
// TODO Auto-generated method stub
}
static void preOrder(Node3 node)
{
if(node==null)
return;
System.out.print(node.data+" ");
preOrder(node.left);
preOrder(node.right);
}
}
class Node3
{
int data;
Node3 left,right;
Node3(int d)
{
this.data=d;
left=right=null;
}
}