/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> result;
TreeNode* node ; //迭代节点
queue<TreeNode*> my_que; //队列
if(root == nullptr) return result;
else // 根节点进队列
{
my_que.push(root);
}
while(my_que.empty() != 1)
{
int size = my_que.size(); //size是不断变化的,指每一层级的点数量
vector<int> nums;//每一层级存放的点
//将每一层的点从队列弹出放入nums,并且下一个层级点放入队列
for(int i=0 ; i<size ; i++)
{
node = my_que.front(); //该层级的点弹出放入数组
my_que.pop();
nums.push_back(node->val);
//每一个弹出点的下一个层级左右节点压入队列
if(node->left != nullptr) my_que.push(node->left);
if(node->right != nullptr) my_que.push(node->right);
}
result.push_back(nums);
}
return result;
}
};