#include
#include
#include
#include
using namespace std;
class node
{
public:
int data;
node* left;
node* right;
node(int d)
{
data=d;
left=NULL;
right=NULL;
}
};
void vertical_line(node* root,int d,map<int,vector > &m) //All the STL’s are passed by value therefore we have used the reference operator…
{
if (root=NULL)
{
return ;
}
m[d].push_back(root->data);
vertical_line(root->left,d-1,m);
vertical_line(root->right,d+1,m);
return;
}
node* buildnode()
{
node* root=new node(3);
node* left_root=new node(2);
node* right_root=new node(1);
root->left=left_root;
root->right=right_root;
node* leftofleft=new node(5);
node* rightofright=new node(6);
left_root->left=leftofleft;
right_root->right=rightofright;
return root;
}
int main()
{
map<int,vector > m;
node* wow=buildnode();
int d=0;
vertical_line(wow,d,m);
for(auto p : m)
{
cout<<p.first<<"–>";
for(int s : p.second)
{
cout<<s;
}
cout<<endl;
}
return 0;
}
please tell me the mistake I am commiting.
Segmentation fault
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.