class Pair{
int line;
TreeNode node;
Pair(int _line, TreeNode _node){
this.line = _line;
this.node = _node;
}
}
public class Solution {
public static List<Integer> getTopView(TreeNode root) {
Queue<Pair> q = new LinkedList<>();
Map<Integer, Integer> map = new TreeMap<>();
List<Integer> list = new ArrayList<>();
q.offer(new Pair(0, root));
while(!q.isEmpty()){
Pair pair = q.poll();
int vl = pair.line; //vl-> vertical line
TreeNode temp = pair.node;
if(map.get(vl) == null){
map.put(vl, temp.data);
}
if(temp.left != null){
q.offer(new Pair(vl-1, temp.left));
}
if(temp.right != null){
q.offer(new Pair(vl+1, temp.right));
}
}
for(Map.Entry<Integer,Integer> entry : map.entrySet()){
list.add(entry.getValue());
}
return list;
}
}