1 /*
2 * @lc app=leetcode.cn id=102 lang=cpp
3 *
4 * [102] 二叉树的层序遍历
5 */
6
7 // @lc code=start
8 /**
9 * Definition for a binary tree node.
10 * struct TreeNode {
11 * int val;
12 * TreeNode *left;
13 * TreeNode *right;
14 * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
15 * };
16 */
17 class Solution {
18 public:
19 vector<vector<int>> levelOrder(TreeNode* root) {
20 vector<vector<int>> res;
21 if(root==nullptr) return {};
22 queue<TreeNode*> q;
23 q.push(root);
24
25 while(!q.empty()){
26 vector<int> tmp;
27
28 int len=q.size();//每一层节点个数 这个没有想到
29 for(int i=0;i<len;i++){//当前层的所有节点出队列,节点值存储在临时数组中,下一层的所有节点全部进队列中
30 TreeNode* node =q.front();
31 q.pop();
32 tmp.push_back(node->val);
33 if(node->left) q.push(node->left);
34 if(node->right) q.push(node->right);
35 }
36 res.push_back(tmp);
37
38 }
39 return res;
40
41 }
42 };
43 // @lc code=end