gfg bottom 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;
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;
}
Also in C++:
- Title
- opencv compile c++
- Category
- C++
- Title
- write to file in C++
- Category
- C++
- Title
- primos menores que
- Category
- C++
- Title
- create new file c++
- Category
- C++
- Title
- c++ string^ to char*
- Category
- C++
- Title
- checking an int in c++
- Category
- C++
- Title
- syntax c++
- Category
- C++
- Title
- pass vector by reference c++
- Category
- C++
- Title
- modulo c++
- Category
- C++
- Title
- converting char to int in c++
- Category
- C++
- Title
- if vector contains value c++
- Category
- C++
- Title
- zeros of array c++
- Category
- C++
- Title
- c++ replace n substrings
- Category
- C++
- Title
- c++ throw exception
- Category
- C++
- Title
- fast input output in c++
- Category
- C++
- Title
- sqrt in c++
- Category
- C++
- Title
- building native binary with il2cpp unity
- Category
- C++
- Title
- c++ remove text file
- Category
- C++
- Title
- convert string to stream c++
- Category
- C++
- Title
- c++ do while loop
- Category
- C++
- Title
- longest common subsequence
- Category
- C++
- Title
- how to create a vector in c++
- Category
- C++
- Title
- mysqli connect
- Category
- C++
- Title
- c++ std::copy to cout
- Category
- C++
- Title
- rick astley - never gonna give you up
- Category
- C++
- Title
- statement that causes a function to end in c++
- Category
- C++
- Title
- swapo algorit
- Category
- C++
- Title
- is TLE means my code is correct but taking more time to computr
- Category
- C++
- Title
- how to sort an array according to another array c++
- Category
- C++
- Title
- how to pass an object by reference in c++
- Category
- C++
- Title
- caesar cipher program in c++
- Category
- C++
- Title
- C++ Student::Student()
- Category
- C++
- Title
- check for bst
- Category
- C++
- Title
- Given an undirected graph, count the number of connected components.
- Category
- C++
- Title
- primeros numeros primos
- Category
- C++
- Title
- how to switch to another branch in git
- Category
- C++
- Title
- insert at position in vector c++
- Category
- C++
- Title
- nearest integer rounding in c++
- Category
- C++
- Title
- reverse in vector c++
- Category
- C++
- Title
- getting a random letter in c++
- Category
- C++
- Title
- c++ convert int to double
- Category
- C++
- Title
- 1d fixed length arrays c++
- Category
- C++
- Title
- c++ string to int
- Category
- C++
- Title
- map vs unordered_map in C++
- Category
- C++
- Title
- flushing output in c++
- Category
- C++
- Title
- what is sigsegv error in c++
- Category
- C++
- Title
- c++ max of array
- Category
- C++
- Title
- loop through array c++
- Category
- C++
- Title
- apple and orange hackerrank solution in c++
- Category
- C++
- Title
- convert GLFWwindow* to IntPtr
- Category
- C++
- Title
- getch c++ library
- Category
- C++
- Title
- new in c++
- Category
- C++
- Title
- c++ get type name of object
- Category
- C++
- Title
- create a dictionary cpp
- Category
- C++
- Title
- c++ read matttrix from text file
- Category
- C++
- Title
- free or delete in c++
- Category
- C++
- Title
- passing array to function c++ pointer
- Category
- C++
- Title
- c++ code 2d block
- Category
- C++
- Title
- c++ loop through int array
- Category
- C++
- Title
- how to convert string into number
- Category
- C++
- Title
- How to find the suarray with maximum sum using divide and conquer
- Category
- C++
- Title
- c++ program for addition of two numbers using functions
- Category
- C++
- Title
- how to iterate over unordered_map c++
- Category
- C++
- Title
- cout console
- Category
- C++
- Title
- how to find the index of an element in a vector c++
- Category
- C++
- Title
- substr c++
- Category
- C++
- Title
- friend function in c++
- Category
- C++
- Title
- clear console c++
- Category
- C++
- Title
- append string to another string c++
- Category
- C++
- Title
- ue4 c++ enum
- Category
- C++
- Title
- memset
- Category
- C++
- Title
- c++ wait for user input
- Category
- C++
- Title
- worker class c++
- Category
- C++
- Title
- ios_base::sync_with_stdio(false);cin.tie(NULL);
- Category
- C++
- Title
- binary representation differ in bits
- Category
- C++
- Title
- convert integer to string c++
- Category
- C++
- Title
- c++ for loop syntax
- Category
- C++
- Title
- power c++
- Category
- C++
- Title
- how to show c++ binary files in sublime text
- Category
- C++
- Title
- inverser les éléments d'un tableau manuellement en c++
- Category
- C++
- Title
- c++ string to integer without stoi
- Category
- C++
- Title
- range based for loop c++
- Category
- C++
- Title
- RLE Encoding/Compression c++
- Category
- C++
- Title
- comparing strings c++
- Category
- C++
- Title
- How to check if a triangular cycle exists in a graph
- Category
- C++
- Title
- nginx linux
- Category
- C++
- Title
- how initilaize deffult value to c++ class
- Category
- C++
- Title
- declare vectors c++
- Category
- C++
- Title
- transpose matrix eigen c++
- Category
- C++
- Title
- iostream library in cpp
- Category
- C++
- Title
- reverse a linked list using recursion
- Category
- C++
- Title
- unordered_set c++
- Category
- C++
- Title
- kruskal's algorithm c++ hackerearth
- Category
- C++
- Title
- char* to int in cpp
- Category
- C++
- Title
- basic ex of maps in c++
- Category
- C++
- Title
- c++ loop through array
- Category
- C++
- Title
- create a bitset of 1024 bits,
- Category
- C++
- Title
- convert int to string c++
- Category
- C++
- Title
- cpp create lambda with recursion
- Category
- C++
- Title
- c++ movment
- Category
- C++