> For the complete documentation index, see [llms.txt](https://dfine.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dfine.gitbook.io/leetcode/590.n_ary_tree_postorder_traversal.md).

# 590.N Ary Tree Postorder Traversal

**590.N Ary Tree Postorder Traversal**

难度:Easy

> 给定一个 N 叉树，返回其节点值的后序遍历。

例如，给定一个 3叉树 : ![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/10/12/narytreeexample.png)

返回其后序遍历: \[5,6,3,2,4,1].

说明: 递归法很简单，你可以使用迭代法完成此题吗?

* 递归法：

```
/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/
class Solution {
public:
    vector<int> postorder(Node* root) {
        vector<int>res;
        if(!root) return res;
        for(auto i:root->children)
        {
            vector<int>tmp =postorder(i);
            res.insert(res.end(),tmp.begin(),tmp.end());
            }
        res.push_back(root->val);
        return res;
    }
};
```

* 迭代法：

```
/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/
class Solution {
public:
    vector<int> postorder(Node* root) {
        vector<int>res;
        if(!root) return res;
        stack<Node*>pt;
        pt.push(root);
        while(!pt.empty())
        {
            if(pt.top()->children.empty())
            {
                res.push_back(pt.top()->val);
                pt.pop();
                if(!pt.empty()) pt.top()->children.erase(pt.top()->children.begin());
            }
            else{
                pt.push(pt.top()->children[0]);
            }
        }
        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, and the optional `goal` query parameter:

```
GET https://dfine.gitbook.io/leetcode/590.n_ary_tree_postorder_traversal.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
