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 ...
随机推荐
- db2 函数、存储过程示例
1.函数 --drop function getMaxDate; create FUNCTION getMaxDate (y int, m int ) returns date begin DECLA ...
- 【目标检测】R-CNN系列与SPP-Net总结
目录 1. 前言 2. R-CNN 2.0 论文链接 2.1 概述 2.2 pre-training 2.3 不同阶段正负样本的IOU阈值 2.4 关于fine-tuning 2.5 对文章的一些思考 ...
- POJ2739解题报告
2017-09-01 17:04:45 writer:pprp 一开始读错题了,总是想不到,其实不是很难,但是就是心理太着急了,反而浪费了很长时间 /* @param:poj2739 @writer: ...
- C++输入/输出流
2017-08-17 09:03:28 writer:pprp 基本的输入/输出流 默认情况下,输入操作会丢弃前导空白,读取数据,遇到空白的时候停止读入: 如果希望的如包括空白在内的任意字符,可以使用 ...
- Flume NG初次使用
一.什么是Flume NG Flume是一个分布式.可靠.和高可用性的海量日志采集.聚合和传输的系统,支持在日志系统中定制各类数据发送方,用于收集数据:同时Flume提供对数据的简单处理,并写到各种数 ...
- 测试php语句执行时间
$start = microtime(true); $elapsed = microtime(true) - $start; echo "That took $elapsed seconds ...
- P3600 随机数生成器
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- windows向github提交代码
随便写的,留给自己看. 一.在github上注册并建立自己的仓库http://www.cnblogs.com/keZhenxu94/p/5288488.html 二.安装windows版本git界面工 ...
- 为什么我们选择parquet
说明:此方案已经我们已经运行1年. 1.场景描述: 我们对客户登录日志做了数据仓库,但实际业务使用中有一些个共同点, A 需要关联维度表 B 最终仅取某个产品一段时间内的数据 C 只关注其中极少的 ...
- python基础方法
一.忽略大小写相等upper(),lower() def cmp(str1,str2): return str1.upper()==str2.upper() list1 = 'MAC' list2 = ...