问题

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

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

例如,给定一个 3叉树 :

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

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


Given an n-ary tree, return the preorder traversal of its nodes' values.

For example, given a 3-ary tree:

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

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


示例

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

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 = Preorder(root);
ShowArray(res); res = Preorder2(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> Preorder(Node root) {
var list = new List<int>();
Pre(root, ref list);
return list;
} public static void Pre(Node root, ref List<int> list) {
if(root == null) return;
list.Add(root.val);
if(root.children == null) return;
foreach(var child in root.children) {
Pre(child, ref list);
}
return;
} public static IList<int> Preorder2(Node root) {
var list = new List<int>();
if(root == null) return list;
var stack = new Stack<Node>();
stack.Push(root);
while(stack.Any()) {
var pop = stack.Pop();
list.Add(pop.val);
if(pop.children != null) {
for(var i = pop.children.Count() - 1; i >= 0; i--)
stack.Push(pop.children[i]);
}
}
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/4090 访问。

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

分析:

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

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

  1. LeetCode 144. 二叉树的前序遍历(Binary Tree Preorder Traversal)

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

  2. LeetCode 589. N叉树的前序遍历(N-ary Tree Preorder Traversal)

    589. N叉树的前序遍历 589. N-ary Tree Preorder Traversal LeetCode589. N-ary Tree Preorder Traversal 题目描述 给定一 ...

  3. Java实现 LeetCode 589 N叉树的前序遍历(遍历树)

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

  4. leecode刷题(28)-- 二叉树的前序遍历

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

  5. 589. N叉树的前序遍历

    [题目] 给定一个 N 叉树,返回其节点值的前序遍历. 例如,给定一个 3叉树 : 返回其前序遍历: [1,3,5,6,2,4]. [解析] """ # Definiti ...

  6. 【LeetCode 144_二叉树_遍历】Binary Tree Preorder Traversal

    解法一:非递归 vector<int> preorderTraversal(TreeNode* root) { vector<int> res; if (root == NUL ...

  7. C#LeetCode刷题-树

    树篇 # 题名 刷题 通过率 难度 94 二叉树的中序遍历   61.6% 中等 95 不同的二叉搜索树 II   43.4% 中等 96 不同的二叉搜索树   51.6% 中等 98 验证二叉搜索树 ...

  8. LeetCode 144. 二叉树的前序遍历(Binary Tree Preorder Traversal)

    144. 二叉树的前序遍历 144. Binary Tree Preorder Traversal 题目描述 给定一个二叉树,返回它的 前序 遍历. LeetCode144. Binary Tree ...

  9. leetcode刷题总结一

    大四狗找工作,要刷题了,leetcode上面题目比较适合面试算法类题目,也不纯粹为了蒙题,锻炼一下面试类型的思维 Single Number: 有N个数,其中只有一个数出现了一次,其他都是两次,找出那 ...

随机推荐

  1. nginx随机模块—random_index

    nginx 随机模板参数: 这个模块他的作用于只有在location中,具体写法如下 location / { root html/code; andom_index on; #index index ...

  2. Java8之Stream 集合聚合操作集锦(含日常练习Demo)

    Stream 是用函数式编程方式在集合类上进行复杂操作的工具,其集成了Java 8中的众多新特性之一的聚合操作,开发者可以更容易地使用Lambda表达式,并且更方便地实现对集合的查找.遍历.过滤以及常 ...

  3. OSCP Learning Notes - Post Exploitation(2)

    Windows Post Exploitation Target Server: IE8-Win 7 VM 1. Download and upload the fgdump, PwDump7, wc ...

  4. Python Ethical Hacking - BACKDOORS(6)

    File Upload: A file is a series of characters. Uploading a file is the opposite of downloading a fil ...

  5. 题解 洛谷 P4177 【[CEOI2008]order】

    进行分析后,发现最大收益可以转化为最小代价,那么我们就可以考虑用最小割来解决这道题. 先算出总收益\(sum\),总收益减去最小代价即为答案. 然后考虑如何建图,如何建立最小割的模型. 发现一个任务最 ...

  6. vue学习(七) v-model 双向数据绑定

    //html <div id="app"> <input type="text"v-model="msg" style=& ...

  7. ⛅剑指 Offer 11. 旋转数组的最小数字

    20207.22 LeetCode 剑指 Offer 11. 旋转数组的最小数字 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个递增排序的数组的一个旋转,输出旋转数组的最小 ...

  8. centos7安装部署docker

    Kubernetes/K8s架构师实战集训营[中级班]:https://pan.baidu.com/s/1FWAz2V7BPsObixlZyW93sw 提取码:mvu0 Kubernetes/K8s架 ...

  9. Python while 循环中使用 else 语句

    Python while 循环中使用 else 语句: else:表示 while 中的语句正常执行完,然后执行 else 语句的部分. 示例: # while 判断条件: # 一行语句 或 多行语句 ...

  10. Python日期和时间_什么是Tick_什么是时间元组_获取当前时间

    Python 日期和时间_什么是 Tick _什么是时间元组: 时间和日期:某年某月某日某时某分某秒 Tick: 时间间隔以 秒 为单位的浮点小数,起始时间为:1970年1月1日0点0分开始 # Ti ...