About Me

About Me : I have been working as a Software Engineer for various international companies for four years.Currently, I am working as a full stack Javascript developer in Petronas(Malaysia).

Skills

Skills • Javascript •Typescript •Python •C •Java •ReactJs • Redux • VueJs • NestJs • React Testing Library • Django• PostgreSQL • MySQL • NodeJs • Git • Docker • Jira • Visual Studio Code • Slack

শুক্রবার, ২২ এপ্রিল, ২০২২

Top View of Binary Tree using JavaScript

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;

    }

}