This code of pre order giving wrong ans of 2,3,4,5,6

public static void preorder(Node node ) {

	if(node == null)
		return;
	System.out.print(node.data + " ");
	 preorder(node.left);
	 preorder(node.right);
	
}

can I talk to you at call because I am not getting it

Hey @ayush_r18,
Please share your code for building the BST.
Your error might lie in the building of the tree.

Hint: Add 1 element at a time while constructing the tree.

public class ConstructBSTQues1 { public static void main(String[] args) { BST a = new BST(); Scanner sc = new Scanner(System.in); System.out.println("Enter val of t: "); int t = sc.nextInt(); while(t-- != 0) { System.out.println("Enter val of n: "); int n = sc.nextInt(); System.out.println("Enter val in arr: "); int[] arr= new int[n]; for(int i =0; i < n; i++) { arr[i] = sc.nextInt(); } Arrays.sort(arr); Node node = a.insert(arr, 0, arr.length-1); a.preorder(node); } }

Remove the lines like these as they aren’t present in the output format

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.