level order traversal is wright but vop is wrong
#include
#include
#include
#include <bits/stdc++.h>
using namespace std;
struct node{
node *left;
int data;
node right;
};
node buildtree(node root){
int d;
cin>>d;
if(root==NULL){
root =new node;
root->data=d;
root->left=NULL;
root->right=NULL;
}
queue<node> p;
p.push(root);
while(!p.empty()){
node *f=p.front();
p.pop();
int c1,c2;
cin>>c1>>c2;
if(c1!=-1){
node *r= new node;
r->data=c1;
r->left=NULL;
r->right=NULL;
f->left=r;
p.push®;
}
if(c2!=-1){
node *r1=new node;
r1->data=c2;
r1->left=NULL;
r1->right=NULL;
f->right=r1;
p.push(r1);
}
}
return root;
}
void printPreorder(node* node)
{
if (node == NULL)
return;
/* first print data of node */
cout << node->data << " ";
/* then recur on left sutree */
printPreorder(node->left);
/* now recur on right subtree */
printPreorder(node->right);
}
void vop(node* root,int d,map<int,vector>&m){
m[d].push_back(root->data);
vop(root->left,d-1,m);
vop(root->left,d+1,m);
}
int main(){
int n;
cin>>n;
struct node *root=NULL;
root=buildtree(root);
map<int,vector>m;
int d=0;
vop(root,d,m);
for(auto it=m.begin();it!=m.end();it++)
{
// cout<first<<" ";
for(int j=0;j<it->second.size();j++)
{
cout<<it->second[j]<<" ";
}
cout<<endl;
}
}