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

这个题目思路就是跟LeetCode questions conlusion_InOrder, PreOrder, PostOrder traversal里面postorder traversal很像, 只是将left child 和right child变成了children而已,

当然这里假设的是children的顺序是从左到右的.

code

1) recursive

class Solution:
def naryPostOrderTraversal(self, root):
def helper(root):
if not root: return
for each in root.children:
helper(each)
ans.append(root.val)
ans = []
helper(root)
return ans

2) iterable

class Solution:
def naryPostOrderTraversal(self, root):
if not root: return []
stack, ans = [(root, False)], []
while stack:
node, visited = stack.pop()
if visited:
ans.append(node.val)
else:
stack.append((node, True))
for each in node.children[::-1]:
stack.append((each, False))
return ans

[LeetCode] 590. N-ary Tree Postorder Traversal_Easy的更多相关文章

  1. 【LEETCODE OJ】Binary Tree Postorder Traversal

    Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...

  2. 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)

    Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...

  3. LeetCode算法题-N-ary Tree Postorder Traversal(Java实现)

    这是悦乐书的第269次更新,第283篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第136题(顺位题号是590).给定一个n-ary树,返回其节点值的后序遍历.例如,给定 ...

  4. 【LeetCode】145. Binary Tree Postorder Traversal 解题报告 (C++&Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

  5. LeetCode解题报告:Binary Tree Postorder Traversal

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

  6. LeetCode OJ 145. Binary Tree Postorder Traversal

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

  7. LeetCode OJ:Binary Tree Postorder Traversal(后序遍历二叉树)

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

  8. 【LeetCode】145. Binary Tree Postorder Traversal

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...

  9. LeetCode题解之N-ary Tree Postorder Traversal

    1.题目描述 2.问题分析 递归. 3.代码 vector<int> postorder(Node* root) { vector<int> v; postNorder(roo ...

随机推荐

  1. D - Replace To Make Regular Bracket Sequence

    You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). ...

  2. PHP基础语法 【】【】 PHP函数

    <?php //注释语法 /* 多行注释 */ //输出语法 echo "hello"; echo "world","dhakj"; ...

  3. linux下安装cmake(安装opencv库)

    apt-get install cmake cmake --version(显示版本号) cmake-gui(打开gui界面) 如果打不该GUI界面时候就需要apt-get install cmake ...

  4. [No0000150]VSVisualStudio提示图标,信号图标的含义

    其右侧的图标表示这是一个接口类型__interface(或者是结构体类型) 其右侧图标表示这是一个类类型 其右侧图标表示这是一个.cpp文件(貌似还可以是.hpp等文件) 其右侧图标表示这是一个枚举类 ...

  5. Some untracked working tree files would be overwritten by checkout. Please move or remove them before you can checkout. View them

    Some untracked working tree files would be overwritten by checkout. Please move or remove them befor ...

  6. 内存不够怎么办? 1.5.1 关于隔离 1.5.2 分段(Segmention) 1.5.3 分页(Paging)

    小结: 1. 内存不够怎么办?1.5.1 关于隔离1.5.2 分段(Segmention)1.5.3 分页(Paging) <程序员的自我修养——链接.装载与库>

  7. 树和二叉树->遍历

    文字描述 二叉树的先根遍历 若二叉树为空,则空操纵,否则 (1) 访问根结点 (2) 先根遍历左子树 (3) 先根遍历右子树 二叉树的中根遍历 若二叉树为空,则空操纵,否则 (1) 中根遍历左子树 ( ...

  8. 内部排序->插入排序->其它插入排序->折半插入排序

    文字描述 和直接插入排序比较,只是把“查找”操作利用“折半查找”来实现,由此进行的插入排序叫做折半插入排序. 示意图 略 算法分析 和直接插入排序比,减少了比较次数,但是移动次数没有变,所以折半插入排 ...

  9. 20165317Java实验三敏捷开发与XP实践

    实验三 敏捷开发与XP实践实验报告 实验目的 安装 alibaba 插件,解决代码中的规范问题.再研究一下Code菜单,找出一项让自己感觉最好用的功能. 在码云上把自己的学习搭档加入自己的项目中,确认 ...

  10. Excel--数据透视图

    原文:https://ke.qq.com/course/289406 1.数据源注意项 2. 3.选中数据源操作 任意选中数据源表格中的单元格(有值得单元格),插入数据透视表 默认数据源区域就是整个表 ...