/**
* Definition for binary tree
* 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;
vector<int> tmp;
if(root==NULL) return ret;
queue<TreeNode*> q;
int count=1;
int level=0;
q.push(root);
while(!q.empty())
{
tmp.clear();
level=0;
for(int i=0;i<count;i++)
{
root=q.front();
q.pop();
tmp.push_back(root->val);
if(root->left!=NULL)
{
q.push(root->left);
level++;
}
if(root->right!=NULL)
{
q.push(root->right);
level++;
}
}
count=level;
ret.push_back(tmp);
}
return ret;
}
};
* Definition for binary tree
* 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;
vector<int> tmp;
if(root==NULL) return ret;
queue<TreeNode*> q;
int count=1;
int level=0;
q.push(root);
while(!q.empty())
{
tmp.clear();
level=0;
for(int i=0;i<count;i++)
{
root=q.front();
q.pop();
tmp.push_back(root->val);
if(root->left!=NULL)
{
q.push(root->left);
level++;
}
if(root->right!=NULL)
{
q.push(root->right);
level++;
}
}
count=level;
ret.push_back(tmp);
}
return ret;
}
};