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?

这道题让我们求N叉树的后序遍历,由于有了之前那道 Binary Tree Postorder Traversal 的基础,了解了二叉树的后序遍历,则N叉树的后序遍历也就没有那么难了。首先还是用递归来做,在递归函数中,判空后,遍历子结点数组,对所有的子结点调用递归函数,然后在 for 循环之外在将当前结点值加入结果 res 数组,这样才能保证是后序遍历的顺序,参见代码如下:

解法一:

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

我们也可以使用迭代的方法来做,这里有个小 trick,写法跟先序遍历十分的像,不同的就是每次把从 stack 中取的结点的值都加到结果 res 的最前面,还有就是遍历子结点数组的顺序是正常的顺序,而前序遍历是从子结点数组的后面往前面遍历,这点区别一定要注意,参见代码如下:

解法二:

class Solution {
public:
vector<int> postorder(Node* root) {
if (!root) return {};
vector<int> res;
stack<Node*> st{{root}};
while (!st.empty()) {
Node *t = st.top(); st.pop();
res.insert(res.begin(), t->val);
for (Node* child : t->children) {
if (child) st.push(child);
}
}
return res;
}
};

类似题目:

Binary Tree Postorder Traversal

N-ary Tree Preorder Traversal

N-ary Tree Level Order Traversal

参考资料:

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

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

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

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

  1. leetcode 590.N-ary Tree Postorder Traversal N叉树的后序遍历

    递归方法 C++代码: /* // Definition for a Node. class Node { public: int val; vector<Node*> children; ...

  2. leetcode 题解:Binary Tree Preorder Traversal (二叉树的先序遍历)

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

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

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

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

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

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

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

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

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

  7. C#LeetCode刷题之#590-N叉树的后序遍历(N-ary Tree Postorder Traversal)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4092 访问. 给定一个 N 叉树,返回其节点值的后序遍历. 例如 ...

  8. (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...

  9. C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)

    145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...

随机推荐

  1. Spark源码剖析 - SparkContext的初始化(一)

    1. SparkContext概述 注意:SparkContext的初始化剖析是基于Spark2.1.0版本的 Spark Driver用于提交用户应用程序,实际可以看作Spark的客户端.了解Spa ...

  2. C++-int类型整数超出范围后的处理

    最近做了一道题目: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: ...

  3. [物理学与PDEs]第4章第2节 反应流体力学方程组 2.1 粘性热传导反应流体力学方程组

    1.  记号: $Z=Z(t,{\bf x})$ 表示未燃气体在微团中所占的百分比 ($Z=1$ 表示完全未燃烧; $Z=0$ 表示完全燃烧). 2.  物理化学 (1)  燃烧过程中, 通过化学反应 ...

  4. python chrome的自定义启动

    # encoding:utf-8 """@version: python27@author: fafa@site: http://www.phpgao.com@softw ...

  5. C# - 表达式与语句

    表达式与语句(Expression&Statement) 操作数(Operands) 1.数字.2.字符.3.变量.4.类型.5.对象.6.方法 操作符(Operator) 参考:C# - 操 ...

  6. 在window系统上安装redis服务-Invalid argument during startup: Failed to open the .conf

    当前redis版本: redis-cli -v redis-cli 3.0.503 网上给的命令(在redis所在文件夹下执行): redis-server –service-install redi ...

  7. unity发布的WebGL部署到IIS

    一.创建WebGL代码 在win7下,Unity3D中发布WebGL,然后部署到IIS,只要代码是对,关键是添加mime类型 二.为网站添加mime类型 .json text/json .unity3 ...

  8. VMware虚拟机安装Linux系统后IP配置(二)

    1.在NAT模式下自动获取IP 2.编辑网卡配置文件(通用) 3.Ctrl+L 清屏.重启网络服务后检查网络是否正常上网 修改后本机IP地址在linux中ping通本机IP 然而在本机却ping不通l ...

  9. Windows caffe 跑mnist实例

       一. 装完caffe当然要来跑跑自带的demo,在examples文件夹下. 先来试试用于手写数字识别的mnist,在 examples/mnist/ 下有需要的代码文件,但是没有图像库. mn ...

  10. OrCAD Capture CIS 16.6 为原理图中的Off-Page Connector添加页面编号

    操作系统:Windows 10 x64 工具1:OrCAD Capture CIS 16.6-S062 (v16-6-112FF) 为原理图中的Off-Page Connector添加页面编号 一般来 ...