# ⭐北邮复试刷题429. N 叉树的层序遍历（按层入队出队BFS）(力扣每日一题)

### `429. N 叉树的层序遍历`

``````示例 1：

``````

### `代码：`

``````/*
// 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>> res = new ArrayList<>();
if(root == null){
return res;
}

queue.offer(root);
List<Integer> list = new ArrayList<>();
while(!queue.isEmpty()){
// 队列想边拿边加时，则利用长度字段；想只拿不加可以用队列判空，最后队列再加；
List<List<Node>> tmpList = new ArrayList<>();
while(!queue.isEmpty()){
// 出队
// 将同层的节点一次拿出
Node tmpQueueOne = queue.poll();
if(tmpQueueOne.children != null)
}
if(tmpList == null || tmpList.size() == 0)
continue;
int tmpLen = tmpList.size();
List<Integer> tmpResOfOne = new ArrayList<>();
for(int i=0;i<tmpLen;i++){
if(tmpList.get(i) != null){
for(int j=0;j<tmpList.get(i).size();j++){
// 入队
// 将本层下层的孩子全部同时入队
queue.offer(tmpList.get(i).get(j));
}
}
}
if(tmpResOfOne == null || tmpResOfOne.size() == 0)
continue;