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. mysql MHA架构搭建过程

    [环境介绍] 系统环境:Red Hat Enterprise Linux 7 + 5.7.18 + MHA version 0.57 系统 IP 主机名 备注 版本 xx系统 192.168.142. ...

  2. [再寄小读者之数学篇](2014-06-20 Beta 函数)

    令 $\dps{B(m,n)=\sum_{k=0}^n C_n^k \cfrac{(-1)^k}{m+k+1}}$, $m,n\in\bbN^+$. (1) 证明 $B(m,n)=B(n,m)$; ( ...

  3. [物理学与PDEs]第5章第5节 弹性动力学方程组及其数学结构

    5.5.1 线性弹性动力学方程组   1.  线性弹性动力学方程组 $$\beex \bea 0&=\rho_0\cfrac{\p{\bf v}}{\p t}-\Div_x{\bf P}-\r ...

  4. spring和mybatis的整合开发(基于MapperFactoryBean的整合开发(方便简单不复杂))

    MapperFactoryBean是mybati-spring团队提供的一个用于根据mapper接口生成mapper对象的类. 在spring配置文件中可以配置以下参数: 1.mapperInterf ...

  5. Codeforces 1060E(dfs计数)

    题目链接 题意 给一棵树,对于一个节点,与它相邻的结点可以连一条边,求所有点对间距离之和 思路 任意两点间的距离被优化为$\left \lceil \frac{s}{2} \right \rceil$ ...

  6. python模块 - pywinauto(windows自动化安装软件)

    GUI 窗口查询工具 spy++lite pywinauto 模块 原理: https://www.cnblogs.com/testlife007/p/4710599.html pywhinayto ...

  7. 万维网WWW详解

    万维网WWW(World Wide Web)并非某种特殊的计算机网络,万维网是一个个大规模的.联机式的信息储藏所,英文简称Web. 万维网使用链接的方式能非常方便地从英特网上的一个站点访问到一个站点, ...

  8. javaWeb之使用servlet搭建服务器入门

    servlet: 百度百科说法: Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,主要功能在于交互式地浏览和修改 ...

  9. js小方法,获取知道公历生日 (‘1992-01-19’),获取阴历生日日期,属相,非简单根据年份判断-----------声明:整理自网络!!

    let lunar = { tg: '甲乙丙丁戊己庚辛壬癸', dz: '子丑寅卯辰巳午未申酉戌亥', number: '一二三四五六七八九十', year: '鼠牛虎兔龙蛇马羊猴鸡狗猪', mont ...

  10. Java Spring Boot VS .NetCore (一)来一个简单的 Hello World

    系列文章 Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filte ...