-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path102.cpp
More file actions
52 lines (47 loc) · 1.4 KB
/
Copy path102.cpp
File metadata and controls
52 lines (47 loc) · 1.4 KB
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
/*******************************************************************************
> File Name: 102.cpp
> Author: sillyplus
> Mail: oi_boy@sina.cn
> Created Time: Tue Mar 8 23:14:50 2016
*******************************************************************************/
/**
* Definition for a binary tree node.
* 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>> ret;
queue<TreeNode*> q;
if (root == NULL)
return ret;
q.push(root);
q.push(NULL);
while (!q.empty()) {
vector<int> tmp;
while (!q.empty()) {
if (q.front() != NULL) {
TreeNode* tn = q.front();
tmp.push_back(tn->val);
if (tn->left != NULL)
q.push(tn->left);
if (tn->right != NULL)
q.push(tn->right);
q.pop();
} else {
q.pop();
if (!q.empty())
q.push(NULL);
ret.push_back(tmp);
break;
}
}
}
return ret;
}
};