题目:
代码(首刷看解析 2024年1月24日):
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
if(root == nullptr) return res;
queue<TreeNode*> que;
TreeNode* cur = root;
que.push(cur);
int size = 0;
while (!que.empty()) {
size = que.size();
vector<int> vec;
while (size--) {
TreeNode* node = que.front();
vec.emplace_back(node->val);
que.pop();
if(node->left) que.push(node->left);
if(node->right) que.push(node->right);
}
res.emplace_back(vec);
}
return res;
}
};