[LeetCode&Python] Problem 590. N-ary Tree Postorder Traversal
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?
Recursion Solution:
"""
# Definition for a Node.
class Node(object):
def __init__(self, val, children):
self.val = val
self.children = children
"""
class Solution(object):
def postorder(self, root):
"""
:type root: Node
:rtype: List[int]
"""
def dfs(r):
if not r:
return
else:
for c in r.children:
dfs(c)
result.append(r.val) result=[]
dfs(root)
return result
Iteration Solution:
"""
# Definition for a Node.
class Node(object):
def __init__(self, val, children):
self.val = val
self.children = children
"""
class Solution(object):
def postorder(self, root):
"""
:type root: Node
:rtype: List[int]
""" if not root:
return [] # We can use a deque to store the solution
res=collections.deque()
# We use stack to store all the node
#Every time, we only need to pick the top node in the stack
#and store its value in res
#And then we store its children in the stack. The right-most
#child is stored in the top.
#If our stack is empty, we finish our job.
stack=[root] while stack:
u=stack.pop()
res.appendleft(u.val)
for c in u.children:
stack.append(c) #change deque back to list
return list(res)
[LeetCode&Python] Problem 590. N-ary Tree Postorder Traversal的更多相关文章
- 【Leetcode】【hard】Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- [LeetCode&Python] Problem 427. Construct Quad Tree
We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or ...
- [LeetCode&Python] Problem 226. Invert Binary Tree
Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9 6 3 1 Tr ...
- LeetCode 590. N叉树的后序遍历(N-ary Tree Postorder Traversal)
590. N叉树的后序遍历 590. N-ary Tree Postorder Traversal 题目描述 给定一个 N 叉树,返回其节点值的后序遍历. LeetCode590. N-ary Tre ...
- 590. N-ary Tree Postorder Traversal - LeetCode
Question 590. N-ary Tree Postorder Traversal Solution 题目大意:后序遍历一个树 思路: 1)递归 2)迭代 Java实现(递归): public ...
- LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)
145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...
- 【leetcode_easy】590. N-ary Tree Postorder Traversal
problem 590. N-ary Tree Postorder Traversal 参考 1. Leetcode_easy_590. N-ary Tree Postorder Traversal; ...
- [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 ...
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
随机推荐
- c++ primer plus 第二章 课后题答案
#include<iostream> using namespace std; int main() { cout << "My name is Jiantong C ...
- 一个很棒的Flutter学习资源列表
目录 文章 一开始 HOWTO文档 网站/博客 高级 视频 组件 演示 UI 材料设计 图片 地图 图表 导航 验证 文字和富文本 分析.流量统计 自动构建 风格样式 媒体 音频 视频 语音 存储 获 ...
- Java基础十二--多态是成员的特点
Java基础十二--多态是成员的特点 一.特点 1,成员变量. 编译和运行都参考等号的左边. 覆盖只发生在函数上,和变量没关系. Fu f = new Zi();System.out.println( ...
- Hibernate入门第二课 Hibernate Tools for Eclipse Plugins安装
Hibernate入门第二课 Hibernate Tools for Eclipse Plugins安装 你们可以直接去,http://blog.csdn.net/wei_chong_chong/ar ...
- 雷林鹏分享:Ruby 字符串(String)
Ruby 字符串(String) Ruby 中的 String 对象存储并操作一个或多个字节的任意序列,通常表示那些代表人类语言的字符. 最简单的字符串是括在单引号(单引号字符)内.在引号标记内的文本 ...
- Visio 入门教程
最近做一个新项目,目前在需求确立阶段,所以每天任务是写文档讨论再修改.由于是云端架构设计,避免不了图形图表配合文字说明,需要制作 E-R 图.网络图.时序图.UML 类图等,对比其他可视化图表工具,V ...
- 新学dfs(看懂了)
在N*N的迷宫内,“#”为墙,“.”为路,“s”为起点,“e”为终点,一共4个方向可以走.从左上角((0,0)“s”)位置处走到右下角((n-1,n-1)“e”)位置处,可以走通则输出YES,不可以走 ...
- uinex 常用命令
删除文件夹实例:rm -rf /var/log/httpd/access将会删除/var/log/httpd/access目录以及其下所有文件.文件夹 删除文件使用实例:rm -f /var/log/ ...
- oracle EBS grant 您不具有执行当前操作的足够权限。请与您的系统管理员联系。
解决方式1: Set the profiles of the below three to 'None' 设置以下三个配置文件为无 FND_VALIDATION_LEVELFND 验证层 FND_FU ...
- ThreadPoolExecutor类
首先分析内部类:ThreadPoolExecutor$Worker //Worker对线程和任务做了一个封装,同时它又实现了Runnable接口, //所以Worker类的线程跑的是自身的run方法 ...