DFS,递归或者栈实现.

"""
# Definition for a Node.
class Node:
def __init__(self, val, children):
self.val = val
self.children = children
"""
class Solution:
def postorder(self, root: 'Node') -> List[int]:
if not root:
return []
if not root.children:
return [root.val]
ans=[]
stack=[root]
node=stack[-1]
mark={}
while stack:
if (not node.children) or (mark.get(node.children[0],0)==1):
pop=stack.pop()
mark[pop]=1
ans.append(pop.val)
if not stack:
break
node=stack[-1]
else:
stack.extend(reversed(node.children))
node = stack[-1]
return ans
"""
# Definition for a Node.
class Node:
def __init__(self, val, children):
self.val = val
self.children = children
"""
class Solution:
def postorder(self, root: 'Node') -> List[int]:
if not root:
return []
if not root.children:
return [root.val]
ans=[]
for c in root.children:
ans.extend(self.postorder(c))
return ans+[root.val]

Leetcode 590. N-ary Tree Postorder Traversal的更多相关文章

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

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

  2. 【LEETCODE OJ】Binary Tree Postorder Traversal

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

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

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

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

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

  5. LeetCode OJ 145. Binary Tree Postorder Traversal

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

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

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

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

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

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

  10. LeetCode题解之Binary Tree Postorder Traversal

    1.题目描述 2.问题分析 递归 3.代码 vector<int> postorderTraversal(TreeNode* root) { vector<int> v; po ...

随机推荐

  1. 尝试编辑java程序

    尝试编译java程序 之前在用软件eclipse编译过简单的hello  java程序,因为软件操作简单,后来学会了用命令符来编译程序.代码如下 public class abc     {      ...

  2. mongodb入门很简单(1)

    mongodb简介: 如前边介绍的两个键-值数据库: memcached和redis属于(key/value)数据库: 而mongodb是文档数据库:存储的是文档(Bson->json的二进制) ...

  3. 轻谈Normalize.css

    Normalize.css 是 * ? Normalize.css只是一个很小的CSS文件,但它在默认的HTML元素样式上提供了跨浏览器的高度一致性.相比于传统的CSS reset , Normali ...

  4. Spring @Scheduler使用cron时的执行问题

    主要想弄清使用Spring @Scheduler cron表达式时的两个问题: 同一定时任务,第二次触发时间到了,第一次还没有执行完成时会执行吗? 不同的定时任务,相互之间是否有影响? 结论写在前面: ...

  5. hdu5727

    Necklace SJX has 2*N magic gems. N of them have Yin energy inside while others have Yang energy. SJX ...

  6. spark SQL学习(综合案例-日志分析)

    日志分析 scala> import org.apache.spark.sql.types._ scala> import org.apache.spark.sql.Row scala&g ...

  7. HIVE分组排序问题

    答案: hive> select *,row_number() over (partition by product_no order by start_time desc) from tabl ...

  8. 二、nginx 安装目录详解

    rpm -ql nginx 路径 类型 介绍 /etc/logrotate.d/nginx  配置文件  Nginx 日志轮转,用于logrotate服务日志切割 /etc/nginx /etc/ng ...

  9. org.springframework.stereotype.Service和com.alibaba.dubbo.config.annotation.Service两种service的区别

    这两个Service,都可以在service类头上使用@Service的注解,于是我就写错了,查了半天才发现.他们的区别大概是这个样子的: org.springframework.stereotype ...

  10. javascript 关键词 new都做了写什么

    翻译自stackoverflow:http://stackoverflow.com/questions/1646698/what-is-the-new-keyword-in-javascript ne ...