-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathLevelOrderTraversal_SP.cpp
More file actions
38 lines (35 loc) · 920 Bytes
/
Copy pathLevelOrderTraversal_SP.cpp
File metadata and controls
38 lines (35 loc) · 920 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
void levelorder(Node * root,map<int,vector<pair<int,int> > >&m,int lvl,int hd)
{
if(root==NULL) return;
m[lvl].push_back({hd,root->data});
levelorder(root->left,m,lvl+1,hd-1);
levelorder(root->right,m,lvl+1,hd+1);
}
void printSpiral(Node *root)
{
map<int,vector<pair<int,int> > > m;
int lvl=0;
int hd=0;
levelorder(root,m,lvl,hd);
map<int,vector<pair<int,int> > > :: iterator it;
int temp=0;
for(it=m.begin();it!=m.end();it++)
{
//sort(((*it).second).begin(),((*it).second).end());
if(temp%2==0)
{
for(int i=(*it).second.size()-1;i>=0;i--)
{
cout<<((*it).second)[i].second<<" ";
}
}
else
{
for(int i=0;i<(*it).second.size();i++)
{
cout<<((*it).second)[i].second<<" ";
}
}
temp++;
}
}