590. N叉树的后序遍历

590. N-ary Tree Postorder Traversal

题目描述

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

LeetCode590. N-ary Tree Postorder Traversal

例如,给定一个 3 叉树:

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

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

Java 实现

Iterative Solution

import java.util.LinkedList;
import java.util.List;
import java.util.Stack; class Node {
public int val;
public List<Node> children; public Node() {
} public Node(int _val, List<Node> _children) {
val = _val;
children = _children;
}
} class Solution {
public List<Integer> postorder(Node root) {
List<Integer> result = new LinkedList<>();
if (root == null) {
return result;
}
Stack<Node> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
Node node = stack.pop();
for (Node child:node.children){
stack.push(child);
}
result.add(0, node.val);
}
return result;
}
}

Recursive Solution

import java.util.LinkedList;
import java.util.List; class Node {
public int val;
public List<Node> children; public Node() {
} public Node(int _val, List<Node> _children) {
val = _val;
children = _children;
}
} class Solution {
List<Integer> result = new LinkedList<>(); public List<Integer> postorder(Node root) {
if (root == null) {
return result;
}
List<Node> node = root.children;
for (int i = 0; i < node.size(); i++) {
postorder(node.get(i));
}
result.add(root.val);
return result;
}
}

相似题目

参考资料

LeetCode 590. N叉树的后序遍历(N-ary Tree Postorder Traversal)的更多相关文章

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

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

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

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

  3. LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)

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

  4. 【遍历二叉树】03二叉树的后序遍历【Binary Tree Postorder Traversal】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,返回他的后序遍历的 ...

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

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

  6. 590. N叉树的后序遍历

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

  7. [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 ...

  8. Leetcode590N-ary Tree Postorder TraversalN叉树的后序遍历

    给定一个 N 叉树,返回其节点值的后序遍历. class Node { public: int val; vector<Node*> children; Node() {} Node(in ...

  9. 剑指offer--二叉树的后序遍历

    思路:对于一个二叉树的后序遍历序列来说,最后一个数一定是根节点,然后前面的数中,从最开始到第一个大于根节点的数都是左子树中的数,而后面到倒数第二个数应该都是大于根节点的,是右子树,如果后面的数中有小于 ...

随机推荐

  1. linux 查看内存,free,ps,说明Buffers,Cached,SReclaimable

    查看机器剩余内存free即可,百度就可以轻松查到,主要想说的 查所有进程占用内存情况并排序: ps aux | sort -nk5 k5代表根据RSS排序,k6代表VSZ排序. ----------- ...

  2. 坑:pytest 运行报错unknown hook 'pytest_namespace' in plugin <module 'allure.pytest_plugin'

    右键运行pytest run时报错,原因是pytest版本过高导致的.有时候会遇到在自己本机没问题,拉取服务器代码下来后就出问题了,所以把pytest版本改低就可以,亲测有效,希望对你有帮助 完整报错 ...

  3. CF1207题解

    D 全排列减去坏序列 坏序列分三种,容斥一下就好了 E 比较有意思 \(A=_{i=1}^{100}\{i\},B=_{i=1}^{100}\{i\cdot 2^7\}\),所以\(A_i~xor~ ...

  4. qt access 数据库

    #include <QCoreApplication> #include <QSqlDatabase> #include <QSqlQuery> #include ...

  5. python3 ssl导入失败

    make LibreSSL 2.6.4 and earlier do not provide the necessary APIs /root/Python-3.7.0/build/lib.linux ...

  6. 宝塔php open_basedir restriction in effect

    解决方法一: 1.网站管理的  防跨站攻击去掉勾选,重启网站,清除浏览器缓存 解决方法二:

  7. Problems with Localtime

    http://pytz.sourceforge.net/#problems-with-localtime https://docs.djangoproject.com/en/2.2/topics/i1 ...

  8. Leetcode: Longest Common Subsequence

    Given two strings text1 and text2, return the length of their longest common subsequence. A subseque ...

  9. 【插件式框架探索系列】应用程序域(AppDomain)

    应用程序域(AppDomain)已经不是一个新名词了,只要熟悉.net的都知道它的存在,不过我们还是先一起来重新认识下应用程序域吧,究竟它是何方神圣. 应用程序域 众所周知,进程是代码执行和资源分配的 ...

  10. SeetaFace2 cmake VS2015编译编译

    cmake Selecting Windows SDK version 10.0.17134.0 to target Windows 10.0.18362. == BUILD_VERSION: v2. ...