Why Not getting write answer for tree bottom View:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
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();
//System.out.println();
bt.verticalOrderTraversal();
}
}
class BinaryTree{
private class Node{
int data;
Node left;
Node right;
int value;
int height;
Node(int d,int val,int height){
data=d;
value=val;
this.height=height;
}
}
Node root;
BinaryTree(int a[]){
root= BuildTree(a,0,0,0);
}
public Node BuildTree(int a[],int index,int val,int height){
if(a[index]==-1)
return null;
Node n=new Node(a[index],val,height);
if((2*index+1)<a.length && a[2*index+1]!=-1){
n.left=BuildTree(a,2*index+1,val-1,height+1);
}
if((2*index+2)<a.length && a[2*index+2]!=-1){
n.right=BuildTree(a,2*index+2,val+1,height+1);
}
return n;
}
public void display(){
display(root);
}
private void display(Node root){
if(root==null)
return;
display(root.left);
System.out.print(root.data+" ");
display(root.right);
}
public void verticalOrderTraversal(){
HashMap<Integer, Node> 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).data+" ");
}
System.out.println();
}
private void verticalTraversal(Node root,HashMap<Integer,Node> map){
if(root==null)
return ;
if(map.containsKey(root.value)){
Node max=null;
if(map.get(root.value).height<=root.height)
max=root;
else
max=map.get(root.value);
map.put(root.value,max);
}
else{
map.put(root.value,root);
}
verticalTraversal(root.left,map);
verticalTraversal(root.right,map);
}
}