-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path102.cpp
More file actions
36 lines (36 loc) · 907 Bytes
/
102.cpp
File metadata and controls
36 lines (36 loc) · 907 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
32
33
34
35
36
/**
* 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>>res;
if(!root)
return res;
queue<TreeNode*> q;
q.push(root);
vector<int> tmp;
while(!q.empty()){
int size = q.size();
tmp.clear();
while(size--){
TreeNode* curr = q.front();
q.pop();
tmp.push_back(curr->val);
if(curr->left)
q.push(curr->left);
if(curr->right)
q.push(curr->right);
}
if(tmp.size()>0)
res.push_back(tmp);
}
return res;
}
};