gfg left 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;
}
};
// function to print left view of tree.
void leftView(Node *root)
{
queue<Obj*> q;
q.push(new Obj(root, 0));
map<int,int> m;
while(!q.empty())
{
Obj *ob = q.front();
q.pop();
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));
}
for(auto it=m.begin(); it!=m.end(); it++)
cout << it->second << "\t";
cout << endl;
}
Also in C++:
- Title
- set in c++
- Category
- C++
- Title
- creare array con c++
- Category
- C++
- Title
- memset
- Category
- C++
- Title
- lopping over an array c++
- Category
- C++
- Title
- opencv compile c++
- Category
- C++
- Title
- c++ pi
- Category
- C++
- Title
- add a timer c++
- Category
- C++
- Title
- passing reference in c++
- Category
- C++
- Title
- stringstream in c++
- Category
- C++
- Title
- how to sort a vector in c++
- Category
- C++
- Title
- getch c++ library
- Category
- C++
- Title
- how to find the size of a character array in c++
- Category
- C++
- Title
- how to initialize an struct object in c++
- Category
- C++
- Title
- filling 2d array with 0 c++
- Category
- C++
- Title
- length of string in c++
- Category
- C++
- Title
- class is replace by structure
- Category
- C++
- Title
- first prime numbers
- Category
- C++
- Title
- input a string in c++
- Category
- C++
- Title
- monotonic deque
- Category
- C++
- Title
- sorting of array in c++
- Category
- C++
- Title
- binary search stl in c++
- Category
- C++
- Title
- c++ get type name of object
- Category
- C++
- Title
- c++ constructor
- Category
- C++
- Title
- call by reference c++ example
- Category
- C++
- Title
- residuo en lenguaje c
- Category
- C++
- Title
- c++ short if
- Category
- C++
- Title
- hashing in competitive programming
- Category
- C++
- Title
- c++ program how to let the user choose different game modes
- Category
- C++
- Title
- font awesome bootstrap cdn
- Category
- C++
- Title
- convert int to string c++
- Category
- C++
- Title
- subtracting two large numbers
- Category
- C++
- Title
- free or delete in c++
- Category
- C++
- Title
- expected initializer before 'isdigit'|
- Category
- C++
- Title
- compile c++ linux
- Category
- C++
- Title
- Insert into vector C++
- Category
- C++
- Title
- length of string c++
- Category
- C++
- Title
- dijkstra in c++
- Category
- C++
- Title
- switch statement c++
- Category
- C++
- Title
- how to read and write in a file c++
- Category
- C++
- Title
- comparing strings c++
- Category
- C++
- Title
- bfs in C++
- Category
- C++
- Title
- removing repeated characters in a string c++
- Category
- C++
- Title
- c++ char print fixed
- Category
- C++
- Title
- c++ vector lower_bound index
- Category
- C++
- Title
- c++ declare char
- Category
- C++
- Title
- how to use winmain function
- Category
- C++
- Title
- c++ set add element
- Category
- C++
- Title
- memcmp in cpp
- Category
- C++
- Title
- initialize array c++
- Category
- C++
- Title
- c++ random numbers
- Category
- C++
- Title
- set of vectors c++
- Category
- C++
- Title
- how to show c++ binary files in sublime text
- Category
- C++
- Title
- array syntax in c++
- Category
- C++
- Title
- and or in c++
- Category
- C++
- Title
- trovare il valore massimo in un array c++ w3
- Category
- C++
- Title
- set c++
- Category
- C++
- Title
- pow c++
- Category
- C++
- Title
- how to find length of string in c++
- Category
- C++
- Title
- int max in c++
- Category
- C++
- Title
- c++ read matttrix from text file
- Category
- C++
- Title
- c++ iterate over vector
- Category
- C++
- Title
- system("pause") note working c++
- Category
- C++
- Title
- c++ string to stream
- Category
- C++
- Title
- % operator in c++
- Category
- C++
- Title
- initialization list c++
- Category
- C++
- Title
- loop through words in string c++
- Category
- C++
- Title
- how to convert n space separated integers in c++
- Category
- C++
- Title
- varint index
- Category
- C++
- Title
- while loops
- Category
- C++
- Title
- including cpp header file in c++
- Category
- C++
- Title
- char **
- Category
- C++
- Title
- 1d fixed length arrays c++
- Category
- C++
- Title
- widechartomultibyte
- Category
- C++
- Title
- matrix multiplication c++ eigen
- Category
- C++
- Title
- tokenize string c++
- Category
- C++
- Title
- how to take input in C++ in coding
- Category
- C++
- Title
- array 2d to 1d
- Category
- C++
- Title
- string comparison in c++
- Category
- C++
- Title
- run program until ctrl-d c++
- Category
- C++
- Title
- how to append one vector to another c++
- Category
- C++
- Title
- 2d vector
- Category
- C++
- Title
- graph using djacency matrix c++
- Category
- C++
- Title
- constant variables in c++
- Category
- C++
- Title
- how to convert int to string c++
- Category
- C++
- Title
- heap in cpp stl
- Category
- C++
- Title
- linkedlist implementation in c++
- Category
- C++
- Title
- regexp_like oracle c++
- Category
- C++
- Title
- how to check sqrt of number is integer c++
- Category
- C++
- Title
- nginx linux
- Category
- C++
- Title
- c++ function to find length of array
- Category
- C++
- Title
- Find a element in a map C++
- Category
- C++
- Title
- declaration vs. definition cpp
- Category
- C++
- Title
- private and public in namespace cpp
- Category
- C++
- Title
- initialize 3d vector c++
- Category
- C++
- Title
- best fit algorithm
- Category
- C++
- Title
- deque c++
- Category
- C++
- Title
- C++ sfinae
- Category
- C++
- Title
- sieve of eratosthenes c++
- Category
- C++
- Title
- cube mapping sdl
- Category
- C++
- Title
- Operator overloading in C++ Programming
- Category
- C++