-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathzigzag_traversal.cpp
30 lines (29 loc) · 946 Bytes
/
zigzag_traversal.cpp
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
// problem link: https://practice.geeksforgeeks.org/problems/zigzag-tree-traversal/1#
// using the same code of level order traversal with one slight change
// reversing the output after every alternate levels
vector <int> zigZagTraversal(Node* node)
{
vector<int> ans;
queue<Node *> q;
q.push(node);
int idx=0, temp=0;
while(q.size() != 0)
{
int count = q.size();
for(int i=0; i<count; i++)
{
Node *top = q.front();
q.pop();
if(top != NULL)
{
ans.push_back(top->data);
if(top -> left) q.push(top->left);
if(top -> right) q.push(top->right);
}
}
if(idx%2 != 0) reverse(ans.begin()+temp, ans.end());
idx++;
temp+=count;
}
return ans;
}