-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path102.cc
56 lines (51 loc) · 1.18 KB
/
102.cc
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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> ans;
if (root == nullptr) {
return ans;
}
queue<TreeNode*> q1, q2;
q1.push(root);
while (true) {
if (move_on(q1, q2, ans)) {
return ans;
}
if (move_on(q2, q1, ans)) {
return ans;
}
}
}
bool move_on(queue<TreeNode*> &q1, queue<TreeNode*> &q2, vector<vector<int>> &ans) {
vector<int> vec;
TreeNode* tmp;
while (!q1.empty()) {
tmp = q1.front();
if (tmp->left)
q2.push(tmp->left);
if (tmp->right)
q2.push(tmp->right);
vec.push_back(tmp->val);
q1.pop();
}
ans.push_back(vec);
return q2.empty();
}
};
int main() {
queue<int> q;
q.push(1);
q.pop();
cout << (q.empty()) << endl;
return 0;
}