[LeetCode] 590. N-ary Tree Postorder Traversal_Easy
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的更多相关文章
- 【LEETCODE OJ】Binary Tree Postorder Traversal
Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...
- 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- LeetCode算法题-N-ary Tree Postorder Traversal(Java实现)
这是悦乐书的第269次更新,第283篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第136题(顺位题号是590).给定一个n-ary树,返回其节点值的后序遍历.例如,给定 ...
- 【LeetCode】145. Binary Tree Postorder Traversal 解题报告 (C++&Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- LeetCode解题报告:Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- LeetCode OJ 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- LeetCode OJ:Binary Tree Postorder Traversal(后序遍历二叉树)
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- 【LeetCode】145. Binary Tree Postorder Traversal
Difficulty: Hard More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...
- LeetCode题解之N-ary Tree Postorder Traversal
1.题目描述 2.问题分析 递归. 3.代码 vector<int> postorder(Node* root) { vector<int> v; postNorder(roo ...
随机推荐
- day_6.5 py
Wireshark的使用 2018-6-5 20:16:05 明天学 03
- webpack构建工具快速上手指南
最近在研究react项目,接触到webpack打包工具.刚接触的时候一脸茫然,经过最近的学习,下面我来带大家开启webpack入门之旅. webpack是什么 webpack是近期最火的一款模块加载器 ...
- rx.js 的冷和热观察
http://cn.rx.js.org/manual/overview.html#h213 https://rxjs-cn.github.io/rxjs5-ultimate-cn/content/ho ...
- CentOS和Redhat单用户模式
当系统无法启动时,可能是/etc/fstab挂载错误导致这时候可以进入单用户模式修改配置文件后重启 重启系统出现以下界面按e 选择第二栏按e健 在后面输入1回车回到上一个页面按b健启动 进入单用户模式 ...
- LuoguP3834 【模板】可持久化线段树 1(主席树)|| 离散化
题目:[模板]可持久化线段树 1(主席树) 不知道说啥. #include<cstdio> #include<cstring> #include<iostream> ...
- pytorch的torch.utils.data.DataLoader认识
PyTorch中数据读取的一个重要接口是torch.utils.data.DataLoader,该接口定义在dataloader.py脚本中,只要是用PyTorch来训练模型基本都会用到该接口, 该接 ...
- C和C指针小记(十)-函数
1.函数的定义 函数的定义就是函数体的实现. 语法: 类型 函数名(形式参数) 代码块 函数返回类型和函数名分开写是代码风格的问题,现代语言如swift返回值在函数名和参数表的后面,这样使得某些工程工 ...
- [daily][centos] redhat增加扩展仓库
https://rpmfusion.org/Configuration sudo yum localinstall --nogpgcheck https://download1.rpmfusion.o ...
- HBase 笔记
参考资料:HBase权威指南 一行由若干列组成,若干列又构成一个列族一个列族的所有列存储在同一个底层的存储文件里,这个文件叫HFile列族的数量有限制:一个列族里列的数量没限制谓词删除:例如允许用户只 ...
- 最大似然估计(Maximum likelihood estimation)(通过例子理解)
似然与概率 https://blog.csdn.net/u014182497/article/details/82252456 在统计学中,似然函数(likelihood function,通常简写为 ...