Print Bottom View Of Binary Tree Code Example


Example: gfg bottom 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;          Obj(Node *node, int dist)         {             root = node;             dis = dist;         } };  // bottom view logic below. void bottomView(Node *root) {     queue<Obj*> q;     q.push(new Obj(root, 0));     map<int,int> m;      while(!q.empty())     {         Obj *ob = q.front();         q.pop();          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));     }      for(auto it=m.begin(); it!=m.end(); it++)         cout << it->second << "\t";      cout << endl; }

Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable