与后序方法类似,只需要更改list添加数的位置。
class Solution {
List<Integer> list = new ArrayList();
public List<Integer> preorder(Node root) {
if (root != null) {
list.add(root.val);
for (Node node : root.children) {
preorder(node);
}
}
return list;
}
}