# C#LeetCode刷题之#590-N叉树的后序遍历（N-ary Tree Postorder Traversal）

Given an n-ary tree, return the postorder traversal of its nodes’ values.

For example, given a `3-ary` tree:

Return its postorder traversal as: `[5,6,3,2,4,1]`.

Note: Recursive solution is trivial, could you do it iteratively?

```public class Program {

public static void Main(string[] args) {
var root = new Node(1,
new List<Node> {
new Node(3, new List<Node> {
new Node(5, null),
new Node(6, null)
}),
new Node(2, null),
new Node(4, null)
});

var res = Postorder(root);
ShowArray(res);

res = Postorder2(root);
ShowArray(res);

}

private static void ShowArray(IList<int> array) {
foreach(var num in array) {
Console.Write(\$"{num} ");
}
Console.WriteLine();
}

public static IList<int> Postorder(Node root) {
var list = new List<int>();
Post(root, ref list);
return list;
}

public static void Post(Node root, ref List<int> list) {
if(root == null) return;
if(root.children == null) {
return;
}
foreach(var child in root.children) {
Post(child, ref list);
}
return;
}

public static IList<int> Postorder2(Node root) {
if(root == null) return new List<int>();
var list = new List<int>();
if(root.children != null) {
foreach(var node in root.children) {
}
}
return list;
}

public class Node {
public int val;
public IList<Node> children;
public Node() { }
public Node(int _val, IList<Node> _children) {
val = _val;
children = _children;
}
}

}```

```5 6 3 2 4 1
5 6 3 2 4 1```