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?

这道题让我们求N叉树的前序遍历,有之前那道Binary Tree Preorder Traversal的基础,知道了二叉树的前序遍历的方法,很容易就可以写出N叉树的前序遍历。先来看递归的解法,主要实现一个递归函数即可,判空之后,将当前结点值加入结果res中,然后遍历子结点数组中所有的结点,对每个结点都调用递归函数即可,参见代码如下:

解法一:

class Solution {
public:
vector<int> preorder(Node* root) {
vector<int> res;
helper(root, res);
return res;
}
void helper(Node* node, vector<int>& res) {
if (!node) return;
res.push_back(node->val);
for (Node* child : node->children) {
helper(child, res);
}
}
};

我们也可以使用迭代的解法来做,使用栈stack来辅助,需要注意的是,如果使用栈的话,我们遍历子结点数组的顺序应该是从后往前的,因为栈是后进先出的顺序,所以需要最先遍历的子结点应该最后进栈,参见代码如下:

解法二:

class Solution {
public:
vector<int> preorder(Node* root) {
if (!root) return {};
vector<int> res;
stack<Node*> st{{root}};
while (!st.empty()) {
Node* t = st.top(); st.pop();
res.push_back(t->val);
for (int i = (int)t->children.size() - ; i >= ; --i) {
st.push(t->children[i]);
}
}
return res;
}
};

类似题目:

Binary Tree Preorder Traversal

N-ary Tree Level Order Traversal

N-ary Tree Postorder Traversal

参考资料:

https://leetcode.com/problems/n-ary-tree-preorder-traversal/

https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/147955/Java-Iterative-and-Recursive-Solutions

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] N-ary Tree Preorder Traversal N叉树的前序遍历的更多相关文章

  1. [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  2. LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)

    翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...

  3. C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)

    144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...

  4. 【LeetCode】Binary Tree Preorder Traversal

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  5. LeetCode:144_Binary Tree Preorder Traversal | 二叉树的前序遍历 | Medium

    题目:Binary Tree Preorder Traversal 二叉树的前序遍历,同样使用栈来解,代码如下: struct TreeNode { int val; TreeNode* left; ...

  6. [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  7. [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  8. LeetCode 589 N-ary Tree Preorder Traversal 解题报告

    题目要求 Given an n-ary tree, return the preorder traversal of its nodes' values. 题目分析及思路 题目给出一棵N叉树,要求返回 ...

  9. [LeetCode 题解]: Binary Tree Preorder Traversal

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a bi ...

随机推荐

  1. ES6 数组遍历方法的实战用法总结(forEach,every,some,map,filter,reduce,reduceRight,indexOf,lastIndexOf)

    目录 forEach every some map filter reduce && reduceRight indexOf lastIndexOf 前言 ES6原生语法中提供了非常多 ...

  2. Gram 矩阵与向量到子空间的距离

    设 $W$ 是 $n$ 维 Euclidean 空间 $V$ 的子空间, $\beta\in V$, 定义 $\beta$ 到 $W$ 的距离  $$\bex  \rd (\beta,W)=|\bet ...

  3. 3.让linux 增加 wget 命令

    Wget主要用于下载文件,在安装软件时会经常用到   直接执行命令 : sudo yum -y install wget   就可以使用wget了

  4. 解决-webkit-box-orient: vertical;(文本溢出)属性在webpack打包后无法编译的问题

    在scss里面: /*! autoprefixer: off */-webkit-box-orient: vertical;/* autoprefixer: on */

  5. 【原创】大数据基础之Mesos(1)简介、安装、使用

    Mesos 1.7.1 官方:http://mesos.apache.org/ 一 简介 Program against your datacenter like it’s a single pool ...

  6. Visual Studio 2015 NuGet Update-Package 失败/报错:Update-Package : Unable to load the service index for source https://api.nuget.org/v3/index.json.

    起因 为了用VS2015 community中的NuGet获取Quartz,在[工具]-[NuGet包管理器]-[程序包管理器控制台]中执行 Install-Package Quartz. 却报如下错 ...

  7. python之配置日志的三种方式

    以下3种方式来配置logging: 1)使用Python代码显式的创建loggers, handlers和formatters并分别调用它们的配置函数: 2)创建一个日志配置文件,然后使用fileCo ...

  8. RPC原理及实现

    一.什么是RPC RPC 的全称是 Remote Procedure Call 是一种进程间通信方式.它允许程序调用另一个地址空间(通常是共享网络的另一台机器上)的过程或函数,而不用程序员显式编码这个 ...

  9. Codeforces 431E Chemistry Experiment 线段树 + 二分

    Chemistry Experiment 维护一个权值线段树,然后二分答案. #include<bits/stdc++.h> #define LL long long #define LD ...

  10. 在js中网页面写入数据时需要注意的几点

    网页代码 <tbody id="t_gun"> <s:iterator value="gunList" status="st&quo ...