Why the question not working right??
import java.util.ArrayList;
import java.util.Collections;
import java.util.;
import java.io.;
public class Main {
public static void main(String args[])throws Exception{
Scanner sc=new Scanner(System.in);
//int a[]={1,2,3,4,5,6,7,-1,-1,-1,-1,-1,8,-1,-1};
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer(br.readLine());
int n=st.countTokens();
int a[]=new int[n];
int i=0;
while(st.hasMoreTokens()){
a[i]=Integer.parseInt(st.nextToken());
i++;
}
BinaryTree bt=new BinaryTree(a);
//bt.display();
bt.verticalOrderTraversal();
}
}
class BinaryTree{
private class Node{
int data;
Node left;
Node right;
int value;
Node(int d,int val){
data=d;
value=val;
}
}
Node root;
BinaryTree(int a[]){
root= BuildTree(a,0,0);
}
public Node BuildTree(int a[],int index,int val){
Node n=new Node(a[index],val);
if((2*index+1)<a.length && a[2*index+1]!=-1){
n.left=BuildTree(a,2*index+1,val-1);
}
if((2*index+2)<a.length && a[2*index+2]!=-1){
n.right=BuildTree(a,2*index+2,val+1);
}
return n;
}
public void display(){
display(root);
}
private void display(Node root){
if(root==null)
return;
display(root.left);
System.out.println(root.data);
display(root.right);
}
public void verticalOrderTraversal(){
HashMap<Integer,Integer> map=new HashMap<>();
verticalTraversal(root,map);
//System.out.println(map);
ArrayList<Integer> set=new ArrayList<>(map.keySet());
Collections.sort(set);
for(Integer i:set){
System.out.print(map.get(i)+" ");
}
System.out.println();
}
private void verticalTraversal(Node root,HashMap<Integer,Integer> map){
if(root==null)
return ;
map.put(root.value,root.data);
verticalTraversal(root.left,map);
verticalTraversal(root.right,map);
}
}