> For the complete documentation index, see [llms.txt](https://ucan.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ucan.gitbook.io/notes/tree/590.-n-ary-tree-postorder-traversal.md).

# 590. N-ary Tree Postorder Traversal

[Given an n-ary tree, return the postorder traversal of its nodes' values.*Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).*](https://leetcode.com/problems/n-ary-tree-postorder-traversal/)

recursion DFS

```java
/*
// 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;
    }
};
*/

class Solution {
    public List<Integer> postorder(Node root) {
        List<Integer> res = new ArrayList<>();
        dfs(root, res);
        return res;
    }
    private void dfs(Node node, List<Integer> list){
        if (node ==null) return;
        for(Node child : node.children){
            dfs(child,list);
        }
        list.add(node.val);
    }
}
```

Iteration

```java

class Solution {
    public List<Integer> postorder(Node root) {
        LinkedList<Integer> res = new LinkedList<>();
        if (root ==null) return res;
        Stack<Node> stack = new Stack<>();
        stack.add(root);
        while(!stack.isEmpty()){
          Node node = stack.peek();
          if (node.children==null){
              res.add(stack.pop().val);
              continue;
          }         
          for(int i = node.children.size()-1;i>=0;i--){
              stack.push(node.children.get(i));
          }
          node.children=null;
        }
        return res;   
    }
}
```

improvement using Linkedlist instead of Stack

```java
class Solution {
    public List<Integer> postorder(Node root) {
        LinkedList<Integer> res = new LinkedList<>();
        LinkedList<Node> stack = new LinkedList<>();
        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 value
            res.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 child
                stack.add(child);
            }
        }
        return res;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ucan.gitbook.io/notes/tree/590.-n-ary-tree-postorder-traversal.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
