public List<Integer> preorder(Node root) {
LinkedList<Node> queue = new LinkedList<>();
LinkedList<Integer> res = new LinkedList<>();
if (root == null) return res;
queue.add(root);
while (!queue.isEmpty()){
Node node = queue.pollLast();
res.addLast(node.val);
Collections.reverse(node.children);
for (Node item : node.children){
queue.add(item);
}
}
return res;
}
leetcode 589 N叉树的前序遍历
最新推荐文章于 2022-03-11 10:44:12 发布