题目:
代码(首刷自解 2024年1月24日):
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children;
Node() {}
Node(int _val) {
val = _val;
}
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
vector<vector<int>> levelOrder(Node* root) {
vector<vector<int>> res = {};
if(root == nullptr) return res;
queue<Node*> que;
Node* cur = root;
que.push(cur);
int size = 0;
while (!que.empty()) {
size = que.size();
vector<int> vec;
while (size--) {
Node* node = que.front();
que.pop();
vec.emplace_back(node->val);
for(int i = 0; i < node->children.size(); ++i){
que.push(node->children[i]);
}
}
res.emplace_back(vec);
}
return res;
}
};