-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJuly_22.cpp
54 lines (49 loc) · 1.41 KB
/
July_22.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
queue<TreeNode*> q;
vector<vector<int>> v;
if(root == NULL)
return v;
q.push(root);
q.push(NULL);
int i = 0;
int l = 0;
while(!q.empty())
{
TreeNode* temp = q.front();
if(temp)
{
vector<int> x;
while(q.front() != NULL)
{
x.push_back(q.front()->val);
if(temp->left)
q.push(temp->left);
if(temp->right)
q.push(temp->right);
q.pop();
temp = q.front();
}
q.push(NULL);
if(l%2 == 1)
reverse(x.begin(), x.end());
v.push_back(x);
l++;
}
q.pop();
}
return v;
}
};