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 ...
随机推荐
- 20145329《Java程序设计》第六周学习总结
教材学习内容总结 第十章 InputSream与OutputStream Java中,输入串流代表对象为java.io.InputStream实例,输出串流代表对象为java.io.OutputStr ...
- 《JAVA程序设计》第五次实验报告
20145333 实验五 Java网络编程及安全 北京电子科技学院(BESTI)实验报告 课程:Java程序设计 班级:1453 指导教师:娄嘉鹏 实验日期:2016.05.06 18:30-21:3 ...
- 如何使用curl进行网页授权
答:使用curl的-u选项,使用方法如下 curl -u username URL (会提示输入密码)
- Android Studio开发学习 - 1. 添加Activity
1. 项目上点右键,New -> Activity -> Blank Activity 这将生成Activity的 Layout.Class .和相关的配置信息(在AndroidManif ...
- docker shipyard 问题
安装 docker shipyard curl -s https://shipyard-project.com/deploy | bash -s docker machine 创建的虚拟机无法直接运 ...
- .net 获取浏览器Cookie(包括HttpOnly)
网上好不容易找到的,分享+收藏 一.接口文件 using System; using System.ComponentModel; using System.Net; using System.Run ...
- 安装 inotify-tools
摘要 inotify-tools, 是一款google出的用于监控文件系统的软件. 一.软件下载地址官方站点地址:http://inotify-tools.sourceforge.net/仓库地址:h ...
- JAVA8 HashMap 源码阅读
序 阅读java源码可能是每一个java程序员的必修课,只有知其所以然,才能更好的使用java,写出更优美的程序,阅读java源码也为我们后面阅读java框架的源码打下了基础.阅读源代码其实就像再看一 ...
- Location对象常用知识
产品终于上线,后期主要是优化了.在开发过程中用到了很多location对象的知识,趁现在有时间先整理一下. Location 对象存储在 Window 对象的 Location 属性中,可通过wind ...
- [Vue]使用 vue-i18n 切换中英文
1.引入 vue-i18n import Vue from 'vue' import VueI18n from 'vue-i18n' import merge from 'lodash/merge' ...