gfg top view of tree

Solutions on MaxInterview for gfg top view of tree by the best coders in the world

showing results for - "gfg top view of tree"
Cristina
02 Jan 2018
1/* This is not the entire code. It's just the function which implements 
2   bottom view. You need to write required code. */
3
4// Obj class is used to store node with it's distance from parent.
5class Obj
6{
7    public:
8        Node *root;
9        int dis; // distance from parent node. distance of root node will be 0.
10
11        Obj(Node *node, int dist)
12        {
13            root = node;
14            dis = dist;
15        }
16};
17
18void topView(Node *root)
19{
20    queue<Obj*> q;
21    q.push(new Obj(root, 0));
22    map<int,int> m;
23
24    while(!q.empty())
25    {
26        Obj *ob = q.front();
27        q.pop();
28		
29      	/* insert node of unique distance from parent node. ignore repitation 
30           of distance. */
31        if(m.find(ob->dis) == m.end())
32            m[ob->dis] = ob->root->data;
33
34        if(ob->root->left != NULL)
35            q.push(new Obj(ob->root->left, ob->dis-1)); 
36        if(ob->root->right != NULL)
37            q.push(new Obj(ob->root->right, ob->dis+1));
38    }
39
40  	// printing nodes.
41    for(auto it=m.begin(); it!=m.end(); it++)
42        cout << it->second << "\t";
43
44    cout << endl;
45}
queries leading to this page
what is top view of a binary search treetop view of binary search treeprint top view of a binary treerint the top view of the binary tree top view of a binary tree is the set of nodes visible when the tree is viewed from the top given a binary tree 2c print the top view of it top view of a binary treetop view of binary tree pythonbinary tree viewstop view of binary tree gfgtop view of binary tree javabinary tree top view 3ftop view of a tree c 23gfg top view of treetop view of a tree top view of binary tree javascriptwhat is tree top viewtop view of bst in ctop view of binary tree without hashingou are given a pointer to the root of a binary tree print the top view of the binary tree top view of binary tree c 2b 2bgiven a binary tree 2c print the nodes in left to right manner as visible from below the tree cpptop view of a tree algorithm solutiontop view solution java visualizationprint top view of a binary tree using recursionprint top view of bstget top view of a tree javatopview binary treewhat is binary tree top view 3ftree top view codewhats top view of binary treetop view of tree javatop view of the treetop view of a binary tree gfgtop view solution javatopview of binary tree pythontop view of a tree exampletop view of binary tree geeksforgeekstop view of tree 3ftop view of binary tree jsview of treetop view of a tree in javatop view of a binary tree codetop level view of binary treetop view of a tree 3dtop view of tree geekstop view of binary tree practice gfgwhat is top view of binary treeprint binary tree top downviews of a treetree top view javabinary tree right top view top view practice gfgtop view of bttop tree gfgtop view of binary tretop view of a treetop view of binary tree in javaprint nodes in top view of binary treeall view of the tree in dsc 23 binary tree top downtree 3a top viewpicture of binary tree in orderwrite a program to print top view of a binary treetop view of binry treetop view tree gfgtree 3atop view javascript solutionprint nodes in a top view of binary treeprint top view of a binary tree in ctop view recursive javayou are given a pointer to the root of a binary tree print the top view of the binary tree top view means when you look the tree from the top the nodes 2c what you will see will be called the top view of the tree see the example below print top view of a binary tree cprint top view of binary treetop view of a tree gfgtop view of binary treetop view of binary tree recursive pseudocodehow to print a binary top tree c 2b 2btop view of binary tree in c codetop order traversal of binary treetop view of binary tree recursivehow to find top view of a bstget top view of a binary tree javareverse top view binary treetop view binary treetop view of binary tree tech dosetop view of a binary tree in cpptop view of binary tree in ctop of the tree gfgtree 3a top view pythontopview of treetree top viewtop view of tree gfgtop view of a binary tree javawhagt is topview of a treetop view of binary tree gfg solutiontop view of treetop view of binary tree is inorderprint binary tree c 2b 2b top downtop view of binary tree recursive pythonwrite full code to print 360 view 28top 2b bottom 29 of a given binary treegfg top view of tree