-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLC_102.cpp
More file actions
39 lines (39 loc) · 916 Bytes
/
LC_102.cpp
File metadata and controls
39 lines (39 loc) · 916 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
37
38
39
/**
* 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>>v;
vector<int>temp;
int count;
if(root==NULL)
return v;
queue<TreeNode*>q;
q.push(root);
while(!q.empty())
{
count=q.size();
temp.clear();
while(count>0)
{
TreeNode *x=q.front();
q.pop();
temp.push_back(x->val);
if(x->left)
q.push(x->left);
if(x->right)
q.push(x->right);
count--;
}
v.push_back(temp);
}
return v;
}
};