-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path188.cpp
More file actions
64 lines (55 loc) · 1.15 KB
/
Copy path188.cpp
File metadata and controls
64 lines (55 loc) · 1.15 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node *left, *right;
};
Node* newNode(int data)
{
Node* node = new Node;
node->data = data;
node->left = node->right = NULL;
return node;
}
vector <vector <int>> result;
void zigzag(Node* root){
if(!root) return;
queue<Node *> q;
q.push(root);
int f=0;
while(!q.empty()){
int sz=q.size();
vector<int> level;
while(sz--){
Node * x=q.front();
q.pop();
if(x->left) q.push(x->left);
if(x->right) q.push(x->right);
level.push_back(x->data);
}
if(f) reverse(level.begin(),level.end());
f=~f;
result.push_back(level);
}
}
int main()
{
Node* root = newNode(8);
root->left = newNode(3);
root->right = newNode(10);
root->left->left = newNode(1);
root->left->right = newNode(6);
root->right->right = newNode(14);
root->right->right->left = newNode(13);
root->left->right->left = newNode(4);
root->left->right->right = newNode(7);
zigzag(root);
for(int i=0 ; i<result.size() ; i++)
{
for(int j=0 ; j<result[i].size() ; j++)
cout<<result[i][j]<<" ";
cout<<endl;
}
return 0;
}