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;
}
} ;
class Solution {
LinkedList< Integer> list = new LinkedList < > ( ) ;
public List< Integer> postorder ( Node root) {
if ( root == null) return list;
helper ( root) ;
list. add ( root. val) ;
return list;
}
private void helper ( Node root) {
for ( Node node : root. children) {
helper ( node) ;
list. add ( node. val) ;
}
}
}
class Solution {
public List< Integer> postorder ( Node root) {
LinkedList< Integer> list = new LinkedList < > ( ) ;
if ( root == null) return list;
Stack< Node> stack = new Stack < > ( ) ;
stack. push ( root) ;
while ( ! stack. isEmpty ( ) ) {
root = stack. pop ( ) ;
list. addFirst ( root. val) ;
for ( Node node : root. children) {
stack. push ( node) ;
}
}
return list;
}
}