/*// Definition for a Node.class Node { public int val; public List<Node> children; public Node() {} public Node(int _val) { val = _val; } public Node(int _val, List<Node> _children) { val = _val; children = _children; }};*/classSolution {publicList<Integer> postorder(Node root) {List<Integer> res =newArrayList<>();dfs(root, res);return res; }privatevoiddfs(Node node,List<Integer> list){if (node ==null) return;for(Node child :node.children){dfs(child,list); }list.add(node.val); }}
classSolution {publicList<Integer> postorder(Node root) {LinkedList<Integer> res =newLinkedList<>();LinkedList<Node> stack =newLinkedList<>();if (root ==null) return res;stack.add(root);while(!stack.isEmpty()){// pop the left most node out Node node =stack.pollLast();// parent value must shown after the children's value// right value must shown after the left valueres.addFirst(node.val);for(Node child:node.children){// add each child node to the end of list// left child will be added before the right childstack.add(child); } }return res; }}