2022-07-23 13:10:25 +02:00
|
|
|
/*
|
|
|
|
// 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 {
|
2024-01-03 12:06:42 +01:00
|
|
|
void levelOrder(vector<vector<int>> &traversal, Node *root, int level) {
|
2022-07-23 13:10:25 +02:00
|
|
|
if (root == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (level >= traversal.size()) {
|
|
|
|
traversal.push_back({});
|
|
|
|
}
|
|
|
|
traversal[level].push_back(root->val);
|
|
|
|
|
|
|
|
for (auto child : root->children) {
|
|
|
|
levelOrder(traversal, child, level + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
public:
|
|
|
|
vector<vector<int>> levelOrder(Node *root) {
|
2022-07-23 13:10:25 +02:00
|
|
|
vector<vector<int>> result;
|
|
|
|
levelOrder(result, root, 0);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|