C#LeetCode刷题之#590-N叉树的后序遍历(N-ary Tree Postorder Traversal)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章
- LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)
题目描述 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路 后 ...
- LeetCode 590. N叉树的后序遍历(N-ary Tree Postorder Traversal)
590. N叉树的后序遍历 590. N-ary Tree Postorder Traversal 题目描述 给定一个 N 叉树,返回其节点值的后序遍历. LeetCode590. N-ary Tre ...
- Java实现 LeetCode 590 N叉树的后序遍历(遍历树,迭代法)
590. N叉树的后序遍历 给定一个 N 叉树,返回其节点值的后序遍历. 例如,给定一个 3叉树 : 返回其后序遍历: [5,6,3,2,4,1]. 说明: 递归法很简单,你可以使用迭代法完成此题吗? ...
- leecode刷题(30)-- 二叉树的后序遍历
leecode刷题(30)-- 二叉树的后序遍历 二叉树的后序遍历 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 思路 ...
- 590. N叉树的后序遍历
给定一个 N 叉树,返回其节点值的后序遍历. 例如,给定一个 3叉树 : 返回其后序遍历: [5,6,3,2,4,1]. 说明: 递归法很简单,你可以使用迭代法完成此题吗? /* // Definit ...
- LeetCode:N叉树的后序遍历【590】
LeetCode:N叉树的后序遍历[590] 题目描述 给定一个 N 叉树,返回其节点值的后序遍历. 例如,给定一个 3叉树 : 返回其后序遍历: [5,6,3,2,4,1]. 题目分析 这道题有好几 ...
- [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 ...
- leecode刷题(29)-- 二叉树的中序遍历
leecode刷题(29)-- 二叉树的中序遍历 二叉树的中序遍历 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 思路 跟 ...
- Leetcode590N-ary Tree Postorder TraversalN叉树的后序遍历
给定一个 N 叉树,返回其节点值的后序遍历. class Node { public: int val; vector<Node*> children; Node() {} Node(in ...
随机推荐
- Cyber Security - Palo Alto Firewall Security Zones
Firewall Security Zones Zones: The foundational aspect of every Firewall. Police network traffic Enf ...
- 为什么SpringBoot项目里引入其他依赖不要写版本号
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> < ...
- Google公布编程语言排名,第一竟然是他?
没想到吧,Python 又拿第一了! 在 Google 公布的编程语言流行指数中,Python 依旧是全球范围内最受欢迎的技术语言! 01 为什么 Python 会这么火? 核心还是因为企业需 ...
- python-闭包和装饰器-01-闭包(closure)
闭包(closure) 闭包就是在一个函数定义的内部再定义一个函数,并且这个函数用到了外边函数的变量,那么将这个函数以及用到的一些变量称之为闭包,如: def line(a, b): def cal( ...
- 初识Elastic search—附《Elasticsearch权威指南—官方guide的译文》
本文作为Elastic search系列的开篇之作,简要介绍其简要历史.安装及基本概念和核心模块. 简史 Elastic search基于Lucene(信息检索引擎,ES里一个index—索引,一个索 ...
- 启动扫描闪退,因为忘了在manifest里申请手机镜头使用许可了。
启动扫描闪退,因为忘了在manifest里申请手机镜头使用许可了.
- Sts 授权直传阿里云 OSS-.net core实现
前言 磁盘怎么又满了?赶紧 快 打电话给运维扩容扩容扩容!这个问题已经是我入职新公司两个月来,第 3 次听到了.经过一通了解,事情原来是这样的.虽然我们使用了阿里云的 OSS 对象存储服务,但是为了不 ...
- 彻底解决ssh.invoke_shell() 返回的中文问题
上一篇:https://www.cnblogs.com/apff/p/9484939.html(python如何实现普通用户登录服务器后切换到root用户再执行命令遇到的错误解决 ) 接上一篇,前两篇 ...
- excel文件双击打开空白
excel文件双击打开之后进入软件,没有去读文件 一.现象描述 打开现象如下所示,只有excel模板,看不到excel中的表格模板. 二.想要打开文件 (1)在软件的文件--->打开--> ...
- Python 字典(Dictionary) items()方法
描述 Python 字典(Dictionary) items() 函数以列表返回可遍历的(键, 值) 元组数组.高佣联盟 www.cgewang.com 语法 items()方法语法: dict.it ...