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;
}
}
}