Failed to pass any test case

"
package assignmnets;

import java.util.Scanner;

public class atdistanceKfromgivenNode {
static boolean vis = false;

public class Node {
	int data;
	Node left;
	Node right;
}

private Node root;

public atdistanceKfromgivenNode(int pre[], int in[]) {
	this.root = construct(pre, 0, pre.length - 1, in, 0, in.length - 1);
}

private Node construct(int[] pre, int plo, int phi, int[] in, int ilo, int ihi) {
	// TODO Auto-generated method stub
	if (plo > phi || ilo > ihi) {
		return null;
	}
	Node nn = new Node();
	nn.data = pre[plo];
	int si = -1;
	for (int i = ilo; i <= ihi; i++) {
		if (in[i] == pre[plo]) {
			si = i;
			break;
		}
	}
	int nel = si - ilo;
	nn.left = construct(pre, plo + 1, plo + nel, in, ilo, si - 1);
	nn.right = construct(pre, plo + nel + 1, phi, in, si + 1, ihi);
	return nn;
}

public void display() {
	display(this.root);
}

private void display(Node root) {
	// TODO Auto-generated method stub
	if (root == null) {
		return;
	}
	String str = "";
	if (root.left != null) {
		str += root.left.data + "->";
	} else {
		str += "- ->";
	}
	str += root.data;
	if (root.right != null) {
		str += "<-" + root.right.data;
	} else {
		str += "<- -";
	}
	System.out.println(str);
	display(root.left);
	display(root.right);
}

public void kfardist(int target, int dst) {
	kfardist(this.root, target, dst) ;
	if(vis==false) {
		System.out.println(0);
	}
	
}

private int kfardist(Node root, int target, int dst) {
	// TODO Auto-generated method stub
	if (root == null) {
		return -1;
	}
	if (root.data == target) {
		kdown(root, 0, dst);
		return 0;
	}
	int ld = kfardist(root.left, target, dst);
	if (ld != -1) {
		if (ld + 1 == 0) {
			System.out.print(root.data + " ");
			vis=true;

		} else {
			kdown(root.right, 0, dst - ld - 2);
		}
		return ld + 1;
	}
	int rd = kfardist(root.right, target, dst);
	if (ld != -1) {
		if (ld + 1 == 0) {
			System.out.print(root.data + " ");
			vis=true;
			
		} else {
			kdown(root.left, 0, dst - rd - 2);
		}
		return rd + 1;
	}
	return -1;
}

private void kdown(Node root, int c, int dst) {
	// TODO Auto-generated method stub
	if (root == null) {
		return;
	}
	if (c == dst) {
		System.out.print(root.data + " ");
		vis=true;
	}
	kdown(root.left, c + 1, dst);
	kdown(root.right, c + 1, dst);

}

public static void main(String[] args) {
	Scanner scn = new Scanner(System.in);
	int n = scn.nextInt();
	int pre[] = new int[n];
	int in[] = new int[n];
	for (int i = 0; i < n; i++) {
		pre[i] = scn.nextInt();
	}
	for (int i = 0; i < n; i++) {
		in[i] = scn.nextInt();
	}
	atdistanceKfromgivenNode m = new atdistanceKfromgivenNode(pre, in);
	int k = scn.nextInt();
	while (k > 0) {
		int target = scn.nextInt();
		int dst = scn.nextInt();
		m.kfardist(target, dst);
		System.out.println();
		k--;
	}

}

}

"

@guptadev354
you need to print the elements in sorted order so instead of printing them you can directly add them to your arrayList and finally sort the arrayList and print the elements.

Even after sorting my list it is showing wrong answer β€œimport java.util.*; public class atdistanceKfromgivenNode { static boolean vis = false; public class Node { int data; Node left; Node right; } private Node root; public atdistanceKfromgivenNode(int pre[], int in[]) { this.root = construct(pre, 0, pre.length - 1, in, 0, in.length - 1); } private Node construct(int[] pre, int plo, int phi, int[] in, int ilo, int ihi) { // TODO Auto-generated method stub if (plo > phi || ilo > ihi) { return null; } Node nn = new Node(); nn.data = pre[plo]; int si = -1; for (int i = ilo; i <= ihi; i++) { if (in[i] == pre[plo]) { si = i; break; } } int nel = si - ilo; nn.left = construct(pre, plo + 1, plo + nel, in, ilo, si - 1); nn.right = construct(pre, plo + nel + 1, phi, in, si + 1, ihi); return nn; } public void display() { display(this.root); } private void display(Node root) { // TODO Auto-generated method stub if (root == null) { return; } String str = β€œβ€; if (root.left != null) { str += root.left.data + β€œ->”; } else { str += β€œ- ->”; } str += root.data; if (root.right != null) { str += β€œ<-” + root.right.data; } else { str += β€œ<- -”; } System.out.println(str); display(root.left); display(root.right); } public void kfardist(int target, int dst, ArrayList list) { kfardist(this.root, target, dst, list); if (vis == false) { System.out.println(0); } } private int kfardist(Node root, int target, int dst, ArrayList list) { // TODO Auto-generated method stub if (root == null) { return -1; } if (root.data == target) { kdown(root, 0, dst, list); return 0; } int ld = kfardist(root.left, target, dst, list); if (ld != -1) { if (ld + 1 == 0) { list.add(root.data); vis = true; } else { kdown(root.right, 0, dst - ld - 2, list); } return ld + 1; } int rd = kfardist(root.right, target, dst, list); if (ld != -1) { if (ld + 1 == 0) { list.add(root.data); vis = true; } else { kdown(root.left, 0, dst - rd - 2, list); } return rd + 1; } return -1; } private void kdown(Node root, int c, int dst, ArrayList list) { // TODO Auto-generated method stub if (root == null) { return; } if (c == dst) { list.add(root.data); vis = true; } kdown(root.left, c + 1, dst, list); kdown(root.right, c + 1, dst, list); } public static void main(String[] args) { Scanner scn = new Scanner(System.in); int n = scn.nextInt(); int pre[] = new int[n]; int in[] = new int[n]; for (int i = 0; i < n; i++) { pre[i] = scn.nextInt(); } for (int i = 0; i < n; i++) { in[i] = scn.nextInt(); } atdistanceKfromgivenNode m = new atdistanceKfromgivenNode(pre, in); int k = scn.nextInt(); while (k > 0) { int target = scn.nextInt(); int dst = scn.nextInt(); ArrayList list = new ArrayList<>(); m.kfardist(target, dst, list); Collections.sort(list); for(int i=0;i<list.size();i++) { System.out.print(list.get(i)+” β€œ); } System.out.println(); k–; } } }”

@guptadev354
here is the correct code

few mistakes in your code

  1. you didn’t initialize the arraylist and you didnt sort it in the end.
  2. when you calculating rd in kdist function you were using ld instead of rd in if condition.
  3. you don’t require vis variable you can easily check the size of the arrayList to in order to print ( 0 ) as the output.

I hope this clears all your doubt, please mark this doubt as resolved in my doubt section and rate me as well. :blush: :blush: