Showing run time error
@Narasimha
//bottom view using recursion
import java.util.*;
import java.util.LinkedList;
public class Main {
static class Node {
Node left;
Node right;
int data;
}
static Node root;
private class Pair {
int data;
int l;
public Pair(int data, int l) {
this.data = data;
this.l = l;
}
}
public void topView(Node n1, TreeMap<Integer, Pair> t1, int dist, int level) {
if (n1 == null) {
return;
}
if (!t1.containsKey(dist) || level >= t1.get(dist).l) {
t1.put(dist, new Pair(n1.data, level));
}
topView(n1.left, t1, dist - 1, level+1);
topView(n1.right, t1, dist + 1, level+1);
}
public static void main(String[] args) {
Main tree = new Main();
Scanner obj = new Scanner(System.in);
LinkedList<Node> l1 = new LinkedList<Node>();
Node n1 = new Node();
tree.root = n1;
n1.data = obj.nextInt();
l1.addLast(n1);
while (!l1.isEmpty()) {
Node temp = l1.removeFirst();
if (temp.data != -1) {
Node templ = new Node();
templ.data = obj.nextInt();
if (templ.data != -1) {
temp.left = templ;
l1.addLast(templ);
} else {
temp.left = null;
}
Node tempr = new Node();
tempr.data = obj.nextInt();
if (tempr.data != -1) {
temp.right = tempr;
l1.addLast(tempr);
} else {
temp.right = null;
}
}
}
TreeMap<Integer, Pair> t1 = new TreeMap<>();
tree.topView(root, t1, 0, 0);
for (int key : t1.keySet()) {
System.out.print(t1.get(key).data + " ");
}
}
}