思路:
// @Title: N 叉树的层序遍历 (N-ary Tree Level Order Traversal)
// @Author: qisiii
// @Date: 2024-09-14 23:32:36
// @Runtime: 3 ms
// @Memory: 43.8 MB
// @comment:
// @flag:
/*
// Definition for a Node.
class Node {
public int val;
public List<Node> children;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, List<Node> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public List<List<Integer>> levelOrder(Node root) {
List<List<Integer>> result = new ArrayList<>();
if (root == null) {
return result;
}
Queue<Node> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
List<Integer> temp = new ArrayList<>();
int size = queue.size();
while (size > 0) {
Node node = queue.poll();
temp.add(node.val);
for(Node child:node.children){
if(child!=null){
queue.offer(child);
}
}
size--;
}
result.add(temp);
}
return result;
}
}