-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path103.cpp
More file actions
31 lines (31 loc) · 908 Bytes
/
Copy path103.cpp
File metadata and controls
31 lines (31 loc) · 908 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
class Solution {
public:
vector<vector<int>> zigzagLevelOrder(TreeNode *root) {
if (!root)
return {};
vector<vector<int>> ret;
bool even = false;
queue<TreeNode *> que;
que.push(root);
while (!que.empty()) {
vector<int> supp = vector<int>();
int size = static_cast<int>(que.size());
while (size > 0) {
auto curr = que.front();
que.pop();
if (!even)
supp.push_back(curr->val);
else
supp.insert(supp.begin(), curr->val);
if (curr->left)
que.push(curr->left);
if (curr->right)
que.push(curr->right);
--size;
}
ret.push_back(supp);
even = !even;
}
return ret;
}
};