问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4092 访问。

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

例如,给定一个 3叉树 :

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

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


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?


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4092 访问。

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); Console.ReadKey();
} 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) {
list.Add(root.val);
return;
}
foreach(var child in root.children) {
Post(child, ref list);
}
list.Add(root.val);
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) {
list.AddRange(Postorder2(node));
}
}
list.Add(root.val);
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;
}
} }

以上给出2种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4092 访问。

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

分析:

显而易见,以上2种算法的时间复杂度均为:  。

C#LeetCode刷题之#590-N叉树的后序遍历(N-ary Tree Postorder Traversal)的更多相关文章

  1. LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)

    题目描述 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路 后 ...

  2. LeetCode 590. N叉树的后序遍历(N-ary Tree Postorder Traversal)

    590. N叉树的后序遍历 590. N-ary Tree Postorder Traversal 题目描述 给定一个 N 叉树,返回其节点值的后序遍历. LeetCode590. N-ary Tre ...

  3. Java实现 LeetCode 590 N叉树的后序遍历(遍历树,迭代法)

    590. N叉树的后序遍历 给定一个 N 叉树,返回其节点值的后序遍历. 例如,给定一个 3叉树 : 返回其后序遍历: [5,6,3,2,4,1]. 说明: 递归法很简单,你可以使用迭代法完成此题吗? ...

  4. leecode刷题(30)-- 二叉树的后序遍历

    leecode刷题(30)-- 二叉树的后序遍历 二叉树的后序遍历 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 思路 ...

  5. 590. N叉树的后序遍历

    给定一个 N 叉树,返回其节点值的后序遍历. 例如,给定一个 3叉树 : 返回其后序遍历: [5,6,3,2,4,1]. 说明: 递归法很简单,你可以使用迭代法完成此题吗? /* // Definit ...

  6. LeetCode:N叉树的后序遍历【590】

    LeetCode:N叉树的后序遍历[590] 题目描述 给定一个 N 叉树,返回其节点值的后序遍历. 例如,给定一个 3叉树 : 返回其后序遍历: [5,6,3,2,4,1]. 题目分析 这道题有好几 ...

  7. [LeetCode] N-ary Tree Postorder Traversal N叉树的后序遍历

    Given an n-ary tree, return the postorder traversal of its nodes' values. For example, given a 3-ary ...

  8. leecode刷题(29)-- 二叉树的中序遍历

    leecode刷题(29)-- 二叉树的中序遍历 二叉树的中序遍历 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 思路 跟 ...

  9. Leetcode590N-ary Tree Postorder TraversalN叉树的后序遍历

    给定一个 N 叉树,返回其节点值的后序遍历. class Node { public: int val; vector<Node*> children; Node() {} Node(in ...

随机推荐

  1. js dom演示

    <body> <div id="div1"> <p name="p1">p1内容</p> <p name= ...

  2. easyUI时间控件

    ##=============================JSP======================================<div class="labelw l ...

  3. OSCP Learning Notes - File Transfers(1)

    File transfer type: 1. HTTP Transfer files through the website. 2.wget wget http://10.0.0.109/exploi ...

  4. C++语法小记---string和int的相互转换

    string和int的相互转换 string转int istringstream is(""); //构造输入字符串流,流的内容初始化为“12”的字符串 int i; is > ...

  5. idea2020安装教程

    2019最新版IDEA亲测可用, 2020最新版IDEA亲测可用, 重要的事说三遍: 如果自己破解不成功建议加群咨询群主:422167709   成功的也可以进群交流 激活码1 N757JE0KCT- ...

  6. vue学习(九) 使用内联样式设置style样式

    /html <div id="app"> //对象就是无序键值对的集合 <h1 :style="{ color:red, 'font-weight':2 ...

  7. 面试题十七:打印从1到最大的n位数

    输入数字n,按顺序打印到最大的n位数 注意:没有规定类型,无论int或long 都会有可能溢出. 应当选择其他类型如String 方法一:定义长度与位数相同的字符数组,从0开始进行加一操作打印 pub ...

  8. java基础(一)注释

    注释的三方方式: 1.多行注释 /* 多行注释01 多行注释02 多行注释03 */

  9. Apache HTTP Server 虚拟主机配置

    Apache HTTP Server 虚拟主机配置(三)     什么是虚拟主机 "虚拟主机"是指在一个机器上运行多个网站(比如:www.company1.com  和 www.c ...

  10. Centos 7 下安装PHP7.2(与Apache搭配的安装方式)

    (1)源码包下载 百度云下载地址:https://pan.baidu.com/s/1xH7aiGYaX62wij4ul5P-ZQ 提取码:m9zc (2)安装php依赖组件: yum -y insta ...