"Print Nodes in Top View of Binary Tree" Code Answer's
You're definitely familiar with the best coding language C++ that developers use to develop their projects and they get all their queries like "Print Nodes in Top View of Binary Tree" answered properly. Developers are finding an appropriate answer about Print Nodes in Top View of Binary Tree related to the C++ coding language. By visiting this online portal developers get answers concerning C++ codes question like Print Nodes in Top View of Binary Tree. Enter your desired code related query in the search bar and get every piece of information about C++ code related question on Print Nodes in Top View of Binary Tree.
gfg top view of tree
/* This is not the entire code. It's just the function which implements
bottom view. You need to write required code. */
// Obj class is used to store node with it's distance from parent.
class Obj
{
public:
Node *root;
int dis; // distance from parent node. distance of root node will be 0.
Obj(Node *node, int dist)
{
root = node;
dis = dist;
}
};
void topView(Node *root)
{
queue<Obj*> q;
q.push(new Obj(root, 0));
map<int,int> m;
while(!q.empty())
{
Obj *ob = q.front();
q.pop();
/* insert node of unique distance from parent node. ignore repitation
of distance. */
if(m.find(ob->dis) == m.end())
m[ob->dis] = ob->root->data;
if(ob->root->left != NULL)
q.push(new Obj(ob->root->left, ob->dis-1));
if(ob->root->right != NULL)
q.push(new Obj(ob->root->right, ob->dis+1));
}
// printing nodes.
for(auto it=m.begin(); it!=m.end(); it++)
cout << it->second << "\t";
cout << endl;
}
All those coders who are working on the C++ based application and are stuck on Print Nodes in Top View of Binary Tree can get a collection of related answers to their query. Programmers need to enter their query on Print Nodes in Top View of Binary Tree related to C++ code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about Print Nodes in Top View of Binary Tree for the programmers working on C++ code while coding their module. Coders are also allowed to rectify already present answers of Print Nodes in Top View of Binary Tree while working on the C++ language code. Developers can add up suggestions if they deem fit any other answer relating to "Print Nodes in Top View of Binary Tree". Visit this developer's friendly online web community, CodeProZone, and get your queries like Print Nodes in Top View of Binary Tree resolved professionally and stay updated to the latest C++ updates.