-
Notifications
You must be signed in to change notification settings - Fork 0
/
102_new.cpp
58 lines (56 loc) · 1.54 KB
/
102_new.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
55
56
57
58
#include<vector>
#include<iostream>
#include<queue>
//#include<unordered_set>
#include<climits>
//Definition for a binary tree node.
typedef struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
} TreeNode;
using namespace std;
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
queue<TreeNode *> qu;
qu.push(root);
while (!qu.empty()) {
int qu_size = qu.size();
int i = 0;
vector<int> data;
while (i++ < qu_size) {
auto qf = qu.front();
data.push_back(qf->val);
if (qf->left) {
qu.push(qf->left);
}
if (qf->right) {
qu.push(qf->right);
}
qu.pop();
}
res.push_back(data);
}
return res;
}
// vector<vector<int>> levelOrder(TreeNode* root) {
// vector<vector<int>> res;
// int level = 0;
// travel(root, level, res);
// return res;
// }
// void travel(TreeNode *root, int level, vector<vector<int>> &res) {
// if (root == NULL) {
// return;
// }
// if (res.size() < level + 1) {
// res.resize(level + 1, vector<int>{});
// }
// res[level].push_back(root->val);
// travel(root->left, level + 1, res);
// travel(root->right, level + 1, res);
// }
};