Failing Test Case for Print keys of BST in the given Range


import java.util.*;
public class Main {

//BST Node
private static class Node
{
	int data; 
	Node left;
	Node right;

}




//BST constructor
private static Node constructBST(int [] A, int n)
{
	Node root = null;
	// Node root = new Node();
	for(int i = 0; i<n; i++)
		root = insert(root, A[i]);

	return root;
}


//insert nodes to form BST
//if root = null, creat a node and return
//if root != null
//case1: if A[i] < root, go to left
//case2: if A[i] > root, go to right;

private static Node insert(Node node, int value)
{
	if(node == null)
	{
		node = new Node();
		node.data = value;
		return node;
	}

	if(value <= node.data)
	{
		node.left = insert(node.left, value);
	}
	//if value > node.data
	else
	{
		node.right = insert(node.right, value);
	}

	return node;
}



//will contain all the values whiich are in the range
private static ArrayList <Integer> ans = new ArrayList <> ();



//preOrder Traversal
private static void preOrder(Node node, int k1, int k2)
{
	if(node == null)
		return;

	if(node.data >= k1 && node.data <= k2)
		ans.add(node.data);

	System.out.print(node.data + " ");
	preOrder(node.left, k1, k2);
	preOrder(node.right, k1, k2);
}



public static void main(String args[]) {
	
	Scanner sc = new Scanner(System.in);

	int t = sc.nextInt();
	while(t-- != 0)
	{
		int n = sc.nextInt();
		int [] A = new int[n];

		//this is a random unsorted array
		for(int i = 0; i<n; i++)
			A[i] = sc.nextInt();

		int k1 = sc.nextInt();
		int k2 = sc.nextInt();

		
		//we have to create a BST wihout sorting the array

		Node root = constructBST(A, n);

		ans.clear();

		System.out.print("# Preorder : ");

		//modified preOrder Traversal 
		preOrder(root, k1, k2);
		
		System.out.println();

		System.out.print("# Nodes within range are : ");
		
		Collections.sort(ans);

		for(int x : ans)
			System.out.print(x + " ");

	}
}

}

Hey @arjunsabu99
code is fine
Add new line each and every Test case :
correct code :
import java.util.*;
public class Main {

//BST Node
private static class Node
{
int data;
Node left;
Node right;

}

//BST constructor
private static Node constructBST(int [] A, int n)
{
Node root = null;
// Node root = new Node();
for(int i = 0; i<n; i++)
root = insert(root, A[i]);

return root;

}

//insert nodes to form BST
//if root = null, creat a node and return
//if root != null
//case1: if A[i] < root, go to left
//case2: if A[i] > root, go to right;

private static Node insert(Node node, int value)
{
if(node == null)
{
node = new Node();
node.data = value;
return node;
}

if(value <= node.data)
{
	node.left = insert(node.left, value);
}
//if value > node.data
else
{
	node.right = insert(node.right, value);
}

return node;

}

//will contain all the values whiich are in the range
private static ArrayList ans = new ArrayList <> ();

//preOrder Traversal
private static void preOrder(Node node, int k1, int k2)
{
if(node == null)
return;

if(node.data >= k1 && node.data <= k2)
	ans.add(node.data);

System.out.print(node.data + " ");
preOrder(node.left, k1, k2);
preOrder(node.right, k1, k2);

}

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

int t = sc.nextInt();
while(t-- != 0)
{
	int n = sc.nextInt();
	int [] A = new int[n];

	//this is a random unsorted array
	for(int i = 0; i<n; i++)
		A[i] = sc.nextInt();

	int k1 = sc.nextInt();
	int k2 = sc.nextInt();

	
	//we have to create a BST wihout sorting the array

	Node root = constructBST(A, n);

	ans.clear();

	System.out.print("# Preorder : ");

	//modified preOrder Traversal 
	preOrder(root, k1, k2);
	
	System.out.println();

	System.out.print("# Nodes within range are : ");
	
	Collections.sort(ans);

	for(int x : ans)
		System.out.print(x + " ");
		System.out.println();

}

}
}