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

例如,给定一个 3叉树 :

 

LeetCode_590.N叉树的后序遍历

 

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

 

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

### C#代码
/*
// Definition for a Node.
public class Node {
    public int val;
    public IList<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, IList<Node> _children) {
        val = _val;
        children = _children;
    }
}
*/
public class Solution {
    private IList<int> list = new List<int>();
    public IList<int> Postorder(Node root) {
        if(root != null){
            if(root.children.Any()){
                foreach(var item in root.children){
                    Postorder(item);
                }
            }
            list.Add(root.val);
        }
        return list;
    }
}

相关文章:

  • 2021-04-19
  • 2021-11-26
  • 2021-11-26
  • 2022-12-23
  • 2021-10-05
  • 2022-12-23
  • 2021-10-13
猜你喜欢
  • 2021-06-17
  • 2021-05-06
  • 2021-07-14
  • 2021-12-26
  • 2022-02-03
  • 2022-01-14
  • 2021-09-28
相关资源
相似解决方案