# 59. N叉树的后序遍历(未使用迭代算法)

java

```java
import java.util.ArrayList;
import java.util.List;

/**
 * 590. N叉树的后序遍历(未使用迭代算法)
 *
 * 给定一个N叉树，返回其节点值的后序遍历。
 *
 * 例如，给定一个 3叉树 :
 *
 * https://leetcode-cn.com/static/images/problemset/NaryTreeExample.png
 *
 * 返回其后序遍历: [5,6,3,2,4,1].
 * 说明: 递归法很简单，你可以使用迭代法完成此题吗?
 */
class Solution590 {

    public List<Integer> postorder(Node root) {
        List<Integer> list=new ArrayList<>();
        todo(root,list);
        return list;
    }

    public void todo(Node root,List<Integer> list) {
        if(root!=null){
            if(root.children!=null){
                for(Node node:root.children){
                    todo(node,list);
                }
            }
            list.add(root.val);
        }
    }

    public static void main(String[] args){
        Node node5=new Node(5,null);
        Node node6=new Node(6,null);
        List<Node> children3=new ArrayList<>();
        children3.add(node5);
        children3.add(node6);

        Node node3=new Node(3,children3);
        Node node2=new Node(2,null);
        Node node4=new Node(4,null);

        List<Node> children1=new ArrayList<>();
        children1.add(node3);
        children1.add(node2);
        children1.add(node4);
        Node root=new Node(1,children1);

        List<Integer> r=new Solution590().postorder(root);
        for(Integer i:r){
            System.out.print(i+" ");
        }

    }
}
```


---

# Agent Instructions: 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://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/shua-ti/shu-ju-jie-gou-lei/59-n-cha-shu-de-hou-xu-bian-li.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.
