class Solution
{
//Function to return a list of nodes visible from the top view
//from left to right in Binary Tree.
topView(root)
{
//your code here
let map = {} ;
function f(root, hdis, level){
if(root === null) return ;
if(map[hdis] === undefined) {
map[hdis] = [level, root.data] ;
}else{
if(level<map[hdis][0]){
map[hdis] = [level, root.data]
}
}
f(root.left, hdis-1, level+1);
f(root.right, hdis +1, level+1);
}
f(root, 0, 0);
// console.log(map)
let arr = [] , ans = [];
for(let [key, value] of Object.entries(map)){
// console.log(value)
arr.push(key);
}
arr.sort((a,b)=>a-b);
// console.log(arr)
for(let i = 0;i<arr.length;i++){
ans.push(map[arr[i]][1])
}
return ans;
}
}