Print all nodes at distance k

Why am i not getting all test cases correct??

import java.util.*;
public class Main {

static Scanner scn = new Scanner(System.in);

public static void main(String[] args) {
    Main m = new Main();
    int n= scn.nextInt();
    int[] pre = takeInput(n);
    int[] in = takeInput(n);
    BinaryTree bt = m.new BinaryTree(pre, in);
    //bt.display();
   // System.out.println("OOOp");
    bt.nfun();
}

public static int[] takeInput(int n) {
   // int n = scn.nextInt();

    int[] rv = new int[n];
    for (int i = 0; i < rv.length; i++) {
        rv[i] = scn.nextInt();
    }
    //System.out.println("DOne");
    return rv;
}

private class BinaryTree {
    private class Node {
        int data;
        Node left;
        Node right;
    }

    private Node root;
    private int size;

    public BinaryTree(int[] pre, int[] in) {
        this.root = this.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) {
        if (plo > phi) {

            return null;

        }

        Node nn = new Node();
        nn.data = pre[plo];

        int j = 0;
        for (int i = ilo; i <= ihi; i++) {
            if (in[i] == pre[plo]) {
                j = i;
                break;
            }
        }

        nn.left = construct(pre, plo + 1, plo + j - ilo, in, ilo, j - 1);
        nn.right = construct(pre, plo + 1 + j - ilo, phi, in, j + 1, ihi);

        return nn;
    }

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

    private void display(Node node) {
        if (node == null) {
            return;
        }

        String str = "";

        if (node.left != null) {
            str += node.left.data;
        } else {
            str += "END";
        }

        str += " => " + node.data + " <= ";

        if (node.right != null) {
            str += node.right.data;
        } else {
            str += "END";
        }

        System.out.println(str);

        this.display(node.left);
        this.display(node.right);
    }
    HashMap<Integer,Node> map1;
    HashMap<Node,Node> map2;
    public  void nfun(){

        int q=scn.nextInt();
        map1=new HashMap<>();
        map2=new HashMap<>();
        fun(this.root,null);
        String str="";
        //System.out.println("sfsdf");
        for(int i=0;i<q;i++){
            int val,k;
            val=scn.nextInt();
            k=scn.nextInt();
            ArrayList<Integer> lst=funs(val,k);
            //System.out.println(lst);
            for(int j=0;j<lst.size();j++){
                str+=lst.get(j)+" ";
            }
            if(lst.size()!=0)
            str+="\n";
        }
        System.out.println(str);
    }
    public  void fun(Node root,Node prev){
        if(root==null)
            return;
        map1.put(root.data,root);
        map2.put(root,prev);
        fun(root.left,root);
        fun(root.right,root);
    }

    public ArrayList<Integer> funs(int value,int k){
        HashMap<Integer,Integer> visited=new HashMap<>();
        Queue<Integer> q=new LinkedList<>();
        q.add(value);
        visited.put(value,1);
        int count=0;
        while(count!=k){
            int n=q.size();
            int gg=0;
            for(int i=0;i<n;i++){
                gg=q.remove();
                if(map1.get(gg).left!=null && !visited.containsKey(map1.get(gg).left.data)){
                    q.add(map1.get(gg).left.data);
                    visited.put(map1.get(gg).left.data,1);
                }
                if(map1.get(gg).right!=null && !visited.containsKey(map1.get(gg).right.data)){
                    q.add(map1.get(gg).right.data);
                    visited.put(map1.get(gg).right.data,1);
                }
                if(map2.get(map1.get(gg))!=null && !visited.containsKey(map2.get(map1.get(gg)).data)){
                    q.add(map2.get(map1.get(gg)).data);
                    visited.put(map2.get(map1.get(gg)).data,1);
                }
            }
            count++;
        }

        int n=q.size();
        ArrayList<Integer> lst=new ArrayList<>();
        for(int i=0;i<n;i++){
            lst.add(q.remove());
        }
        return lst;
    }

}

}

hey @Anubhav44044
Read this line carefully
Output Format
On T lines print space separated desired output for each test case in sorted form

Now also 1 test case not passing

import java.util.*; public class Main { static Scanner scn = new Scanner(System.in); public static void main(String[] args) { Main m = new Main(); int n= scn.nextInt(); int[] pre = takeInput(n); int[] in = takeInput(n); BinaryTree bt = m.new BinaryTree(pre, in); //bt.display(); // System.out.println(β€œOOOp”); bt.nfun(); } public static int[] takeInput(int n) { // int n = scn.nextInt(); int[] rv = new int[n]; for (int i = 0; i < rv.length; i++) { rv[i] = scn.nextInt(); } //System.out.println(β€œDOne”); return rv; } private class BinaryTree { private class Node { int data; Node left; Node right; } private Node root; private int size; public BinaryTree(int[] pre, int[] in) { this.root = this.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) { if (plo > phi) { return null; } Node nn = new Node(); nn.data = pre[plo]; int j = 0; for (int i = ilo; i <= ihi; i++) { if (in[i] == pre[plo]) { j = i; break; } } nn.left = construct(pre, plo + 1, plo + j - ilo, in, ilo, j - 1); nn.right = construct(pre, plo + 1 + j - ilo, phi, in, j + 1, ihi); return nn; } public void display() { this.display(this.root); } private void display(Node node) { if (node == null) { return; } String str = β€œβ€; if (node.left != null) { str += node.left.data; } else { str += β€œEND”; } str += " => " + node.data + " <= β€œ; if (node.right != null) { str += node.right.data; } else { str += β€œEND”; } System.out.println(str); this.display(node.left); this.display(node.right); } HashMap<Integer,Node> map1; HashMap<Node,Node> map2; public void nfun(){ int q=scn.nextInt(); map1=new HashMap<>(); map2=new HashMap<>(); fun(this.root,null); String str=”"; //System.out.println(β€œsfsdf”); for(int i=0;i<q;i++){ int val,k; val=scn.nextInt(); k=scn.nextInt(); ArrayList lst=funs(val,k); Collections.sort(lst); //System.out.println(lst); for(int j=0;j<lst.size();j++){ str+=lst.get(j)+" β€œ; } if(lst.size()!=0) str+=”\n"; else str+=β€œ0\n”; } System.out.println(str); } public void fun(Node root,Node prev){ if(root==null) return; map1.put(root.data,root); map2.put(root,prev); fun(root.left,root); fun(root.right,root); } public ArrayList funs(int value,int k){ HashMap<Integer,Integer> visited=new HashMap<>(); Queue q=new LinkedList<>(); q.add(value); visited.put(value,1); int count=0; while(count!=k){ int n=q.size(); int gg=0; for(int i=0;i<n;i++){ gg=q.remove(); if(map1.get(gg).left!=null && !visited.containsKey(map1.get(gg).left.data)){ q.add(map1.get(gg).left.data); visited.put(map1.get(gg).left.data,1); } if(map1.get(gg).right!=null && !visited.containsKey(map1.get(gg).right.data)){ q.add(map1.get(gg).right.data); visited.put(map1.get(gg).right.data,1); } if(map2.get(map1.get(gg))!=null && !visited.containsKey(map2.get(map1.get(gg)).data)){ q.add(map2.get(map1.get(gg)).data); visited.put(map2.get(map1.get(gg)).data,1); } } count++; } int n=q.size(); ArrayList lst=new ArrayList<>(); for(int i=0;i<n;i++){ lst.add(q.remove()); } return lst; } } }

@Anubhav44044
Print 0 if no such node exist at distance k.
try for this input
4
60 65 50 70
50 65 60 70
1
70 4
correct output : 0