590. N-ary Tree Postorder Traversal

590. N-ary Tree Postorder Traversal

/*
// Definition for a Node.
class Node {
    public int val;
    public List children;

    public Node() {}

    public Node(int _val,List _children) {
        val = _val;
        children = _children;
    }
};
*/
class Solution {
   	public List postorder(Node root) {
		List out = new ArrayList();
		postorder(root, out);
		return out;
	}

	private void postorder(Node root, List out) {
		if( null != root){
		for (int i = 0; i < root.children.size(); i++)
			postorder(root.children.get(i), out);
		out.add(root.val);
        }
	}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s