Why am I getting a TLE for this code?
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int testCases = sc.nextInt();
for(int test=1; test<=testCases; test++) {
int n = sc.nextInt();
int[] arr = new int[n];
for(int i=0; i<n; i++) {
arr[i] = sc.nextInt();
}
BST build = new BST();
build.construct(arr);
build.preOrder();
}
}
}
class BST {
private class Node{
int data;
Node left;
Node right;
}
private Node root;
public void construct(int[] arr) {
this.root = construct(arr, 0, arr.length-1);
}
private Node construct(int[] arr, int low, int high) {
//base case
if(low > high) {
return null;
}
//mid
int mid = (low+high)/2;
//create new node
Node nn = new Node();
nn.data = arr[mid];
nn.left = construct(arr, low, mid-1);
nn.right = construct(arr, mid+1, high);
return nn;
}
public void preOrder(){
preOrder(this.root);
}
private void preOrder(Node node) {
if(node == null) {
return ;
}
System.out.print(node.data + " ");
preOrder(node.left);
preOrder(node.right);
}
}