/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
class Solution {
public:
/**
*
* @param root TreeNode类
* @return int整型vector<vector<>>
*/
vector<vector<int> > levelOrder(TreeNode* root) {
queue<TreeNode*> que;
que.push(root);
vector<vector<int> > ans;
if(!root) return ans;
while(!que.empty()){
//size记录某一层的元素个数
int size = que.size();
vector<int> tempv;
for(int i=0; i<size; i++){
//队列先进先出,front只是取元素,还需要pop删除第一个元素
TreeNode* temp = que.front();
que.pop();
tempv.push_back(temp->val);
if(temp->left){
que.push(temp->left);
}
if(temp->right){
que.push(temp->right);
}
}
ans.push_back(tempv);
}
return ans;
}
};