-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP103.cpp
42 lines (36 loc) · 1.09 KB
/
P103.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
#include "header.h"
class Solution {
public:
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> res={};
if (!root) return res;
bool order = true;
vector<TreeNode*> que;
que.push_back(root);
while (!que.empty()) {
vector<int> ans;
vector<TreeNode*> next;
for (size_t i=0;i<que.size();++i) {
ans.push_back(que[i]->val);
if (que[i]->left) next.push_back(que[i]->left);
if (que[i]->right) next.push_back(que[i]->right);
}
if (!order)
reverse(ans.begin(), ans.end());
order = !order;
res.push_back(ans);
que=next;
}
return res;
}
};
int main() {
TreeNode* root = new TreeNode(0);
auto res = Solution().zigzagLevelOrder(root);
for (auto l:res) {
cout << endl;
for (auto ele:l)
cout << ele << " ";
}
return 0;
}