classSolution {publicList<Integer> preorder(Node root) {// use stack to mock dfsStack<Node> stack =newStack<>();List<Integer> res =newArrayList<>();if (root ==null) return res;stack.add(root);while (!stack.isEmpty()){Node node =stack.pop();res.add(node.val);// list support iterate from end to headfor (int i=node.children.size()-1; i>=0; i--){stack.add(node.children.get(i)); } }return res; }}