gfg top view of tree
C++
/* 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;
}
Also in C++:
- Title
- check for bst
- Category
- C++
- Title
- inheritance protected in c++
- Category
- C++
- Title
- can you use rand to read in from an external file inc++
- Category
- C++
- Title
- Convert binary tree to a doubly linked list
- Category
- C++
- Title
- making random numbers in c++
- Category
- C++
- Title
- bool function in c++
- Category
- C++
- Title
- convert stirng to int c++
- Category
- C++
- Title
- fast input output in c++
- Category
- C++
- Title
- leveling system c++
- Category
- C++
- Title
- c++ ros subscriber
- Category
- C++
- Title
- convert decimal to binary in c++
- Category
- C++
- Title
- inverser les éléments d'un tableau manuellement en c++
- Category
- C++
- Title
- how to sort an array according to another array c++
- Category
- C++
- Title
- how to type cast quotient of two integers to double with c++
- Category
- C++
- Title
- caesar cipher program in c++
- Category
- C++
- Title
- trovare il valore massimo in un array c++ w3
- Category
- C++
- Title
- c++ vector lower_bound index
- Category
- C++
- Title
- powers of 2 in cpp
- Category
- C++
- Title
- modulo c++
- Category
- C++
- Title
- c++ string to stream
- Category
- C++
- Title
- cout console
- Category
- C++
- Title
- c++ read matttrix from text file
- Category
- C++
- Title
- how to initialize a vector in c++
- Category
- C++
- Title
- generate random double c++
- Category
- C++
- Title
- memset
- Category
- C++
- Title
- how to convert string into number
- Category
- C++
- Title
- c++ how to loop through a vector but not the last element
- Category
- C++
- Title
- passing reference in c++
- Category
- C++
- Title
- how to execute c++ program in cmd
- Category
- C++
- Title
- dijkstra c++ geeksforgeeks using set
- Category
- C++
- Title
- char **
- Category
- C++
- Title
- statement that causes a function to end in c++
- Category
- C++
- Title
- map insert c++
- Category
- C++
- Title
- how to create object in c++
- Category
- C++
- Title
- create new file c++
- Category
- C++
- Title
- c++ menu selection with arrow keys
- Category
- C++
- Title
- pop from between string c++
- Category
- C++
- Title
- c++ sql
- Category
- C++
- Title
- free or delete in c++
- Category
- C++
- Title
- getch c++ library
- Category
- C++
- Title
- variadic templates
- Category
- C++
- Title
- c++ program for addition of two numbers using functions
- Category
- C++
- Title
- c++ print one line to console instead of multiple
- Category
- C++
- Title
- c++ get last element in vector
- Category
- C++
- Title
- static variable in c++
- Category
- C++
- Title
- bitset c++
- Category
- C++
- Title
- c++ give options
- Category
- C++
- Title
- Read multiple files(.txt) c++
- Category
- C++
- Title
- exponenciacion binaria
- Category
- C++
- Title
- unordered_map c++ insert
- Category
- C++
- Title
- c++ while true loop
- Category
- C++
- Title
- counting valleys hackerrank solution in c++
- Category
- C++
- Title
- how to load from files C++
- Category
- C++
- Title
- power in c++
- Category
- C++
- Title
- deque c++
- Category
- C++
- Title
- std string find character c++
- Category
- C++
- Title
- c++ for loop syntax
- Category
- C++
- Title
- how to create a vector in c++
- Category
- C++
- Title
- map.erase in c++
- Category
- C++
- Title
- c++ while true
- Category
- C++
- Title
- file objects in c++
- Category
- C++
- Title
- how to cout in c++
- Category
- C++
- Title
- reverse in vector c++
- Category
- C++
- Title
- c++ isalphanum
- Category
- C++
- Title
- for loop
- Category
- C++
- Title
- c++ loop through array
- Category
- C++
- Title
- merge sort in c++
- Category
- C++
- Title
- cloud hosting
- Category
- C++
- Title
- how to format decimal palces in c++
- Category
- C++
- Title
- ios_base::sync_with_stdio(false);cin.tie(NULL);
- Category
- C++
- Title
- how use global variables instead of local in c++
- Category
- C++
- Title
- how to print for limited decimal values in c++
- Category
- C++
- Title
- prims c++
- Category
- C++
- Title
- iostream library in cpp
- Category
- C++
- Title
- set lower bound c++
- Category
- C++
- Title
- how to check datatype of a variable in c++
- Category
- C++
- Title
- gcd in c++
- Category
- C++
- Title
- c++ switch case statement
- Category
- C++
- Title
- recursive in c++
- Category
- C++
- Title
- map arduino
- Category
- C++
- Title
- c++ std::copy to cout
- Category
- C++
- Title
- subarray sum in c++
- Category
- C++
- Title
- how to switch to another branch in git
- Category
- C++
- Title
- switch statement c++
- Category
- C++
- Title
- how to delete an element in vector pair in cpp
- Category
- C++
- Title
- two sum problem in c++
- Category
- C++
- Title
- solve linear equations geeksforgeeks
- Category
- C++
- Title
- c++ typeid get type name
- Category
- C++
- Title
- delete a double pointer c++
- Category
- C++
- Title
- create a dictionary cpp
- Category
- C++
- Title
- Application of c++ in youtube program
- Category
- C++
- Title
- c++ code 2d block
- Category
- C++
- Title
- multiset c++
- Category
- C++
- Title
- C++ string format ctime
- Category
- C++
- Title
- assegnare valori in c++
- Category
- C++
- Title
- mysqli connect
- Category
- C++
- Title
- number of islands leetcode code
- Category
- C++
- Title
- binary search in set c++
- Category
- C++
- Title
- c++ string to vector int
- Category
- C++
- Title
- calling a method on an object c++
- Category
- C++