public List<List<Integer>> levelOrder(Node root) {List<List<Integer>> list = new LinkedList<>();Deque<Node> deque = new LinkedList();if (root != null) {deque.addLast(root);}while (!deque.isEmpty()) {LinkedList<Integer> tempList = new LinkedList<>();// 队列长度int size = deque.size();while (size-- > 0) {Node poll = deque.pollFirst();for (Node child : poll.children) {deque.addLast(child);}tempList.add(poll.val);}list.add(tempList);}return list;}
public List<List<Integer>> levelOrder(Node root) {List<List<Integer>> ans = new ArrayList<>();dfs(root, ans, 0);return ans;}void dfs(Node node, List<List<Integer>> ans, int level) {if (node == null) return;if (ans.size() == level) ans.add(new ArrayList<>());ans.get(level).add(node.val);for (Node child : node.children) dfs(child, ans, level + 1);}
