ð 429. N åæ çå±åºéå
2022幎10æ10æ¥
- algorithm
ð 429. N åæ çå±åºéå
éŸåºŠ: ð
é®é¢æè¿°
解æ³
/*
// 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;
}
LinkedList<Node> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()) {
int len = queue.size();
List<Integer> list = new ArrayList<>();
for(int i = 0; i < len; i ++) {
Node cur = queue.poll();
list.add(cur.val);
// å°è¯æ·»å cur çåèç¹å
¥é
for(Node c: cur.children) {
queue.offer(c);
}
}
res.add(list);
}
return res;
}
}