Leetcode 590. N-ary Tree Postorder Traversal
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的更多相关文章
- 【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 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 解题报告 (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算法题-N-ary Tree Postorder Traversal(Java实现)
这是悦乐书的第269次更新,第283篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第136题(顺位题号是590).给定一个n-ary树,返回其节点值的后序遍历.例如,给定 ...
- 【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 ...
- LeetCode题解之Binary Tree Postorder Traversal
1.题目描述 2.问题分析 递归 3.代码 vector<int> postorderTraversal(TreeNode* root) { vector<int> v; po ...
随机推荐
- 超详细 Spring @RequestMapping 注解使用技巧 (转)
@RequestMapping 是 Spring Web 应用程序中最常被用到的注解之一.这个注解会将 HTTP 请求映射到 MVC 和 REST 控制器的处理方法上. 在这篇文章中,你将会看到 @R ...
- 快用Visual Studio(三)- 代码重构
什么是代码重构 编写代码 | 找到代码 | 修改代码 关于重构的工具 Bracket Matching Selection Cursors Intelligence Parameter hints E ...
- [专业亲测]Ubuntu16.04安装Nvidia显卡驱动(cuda)--解决你的所有困惑【转】
本文转载自: 因为要做毕设需要安装caffe2,配置cuda8.0,但是安装nvidia驱动真的是把我难倒了,看了很多篇博文都没有效果,现在我自己重新总结了下几种 安装方法(亲测有效),希望能帮到大家 ...
- IE8下打印内容缩小问题
去掉启动缩小字体填充的设置项勾选 来自为知笔记(Wiz)
- Entity Framework 中 Schema是什么
在使用Entity Framework时,会注意到下面这句: protected override void OnModelCreating(DbModelBuilder modelBuilder) ...
- photoshop cc下载与安装
地址:https://jingyan.baidu.com/article/c275f6bacdd927e33d756729.html
- django教程目录
什么是web框架? Do a web framework ourselves MVC和MTV模式 django的流程和命令行工具 Django的配置文件(settings) Django URL (路 ...
- 已知起始点,获取每段等距离途经点的经纬度(用百度js api作)
已知两个中文地址,自动规划路径,获取路径上每个3公里的点的经纬度 <html> <head> <meta http-equiv="Content-Type&qu ...
- [oracle] DBLINK +同义词,实现本地数据库访问另一台机器的数据库
起因:订单表原来在90库上,后各种原因移到了40库上,需访问40库上的订单表.采用DBLINK+同义词方法: -- 1 在90机器上用GPSV4登录PLSQL,创建DBLINK,从本地数据库,连接到远 ...
- linux下如何安装lua
1.下载lua包并解压 wget -c http://www.lua.org/ftp/lua-5.3.0.tar.gz tar zxvf lua-5.3.0.tar.gz 2.下载libreadli ...