题目
代码
class Solution {
public List<Integer> preorder(Node root) {
List<Integer> res = new ArrayList<>();
Stack<Node> stack = new Stack<>();
if (root == null) return res;
stack.add(root);
while (!stack.empty()) {
root = stack.pop();
res.add(root.val);
for (int i = root.children.size() - 1; i >= 0; i--)
stack.add(root.children.get(i));
}return res;
}
}