[LeetCode&Python] Problem 589. N-ary Tree Preorder Traversal
Given an n-ary tree, return the preorder traversal of its nodes' values.
For example, given a 3-ary tree:

Return its preorder traversal as: [1,3,5,6,2,4].
Note: Recursive solution is trivial, could you do it iteratively?
Recursion Method:
"""
# Definition for a Node.
class Node(object):
def __init__(self, val, children):
self.val = val
self.children = children
"""
class Solution(object):
def preorder(self, root):
"""
:type root: Node
:rtype: List[int]
"""
l=[] def subpreorderfun(r):
if r:
l.append(r.val)
for c in r.children:
subpreorderfun(c) subpreorderfun(root) return l
Iteration Method:
"""
# Definition for a Node.
class Node(object):
def __init__(self, val, children):
self.val = val
self.children = children
"""
class Solution(object):
def preorder(self, root):
"""
:type root: Node
:rtype: List[int]
"""
l=[]
q=[root] if root:
p=[]
while q:
a=q.pop(0)
l.append(a.val) for c in a.children:
p.append(c) n=len(p)
for i in range(n):
q=[p.pop()]+q return l
[LeetCode&Python] Problem 589. N-ary Tree Preorder Traversal的更多相关文章
- 【Leetcode】【Medium】Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- 【leetcode刷题笔记】Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- [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 144]二叉树前序遍历Binary Tree Preorder Traversal
[题目] Given a binary tree, return the preordertraversal of its nodes' values. Example: Input: [1,null ...
- LeetCode 589. N叉树的前序遍历(N-ary Tree Preorder Traversal)
589. N叉树的前序遍历 589. N-ary Tree Preorder Traversal LeetCode589. N-ary Tree Preorder Traversal 题目描述 给定一 ...
- LeetCode 144. 二叉树的前序遍历(Binary Tree Preorder Traversal)
144. 二叉树的前序遍历 144. Binary Tree Preorder Traversal 题目描述 给定一个二叉树,返回它的 前序 遍历. LeetCode144. Binary Tree ...
- 【leetcode_easy】589. N-ary Tree Preorder Traversal
problem 589. N-ary Tree Preorder Traversal N叉树的前序遍历 首先复习一下树的4种遍历,前序.中序.后序和层序.其中,前序.中序和后序是根据根节点的顺序进行区 ...
- [LeetCode] N-ary Tree Preorder Traversal N叉树的前序遍历
Given an n-ary tree, return the preorder traversal of its nodes' values. For example, given a 3-ary ...
随机推荐
- Spring AMQP 源码分析 07 - MessageListenerAdapter
### 准备 ## 目标 了解 Spring AMQP 如何用 POJO 处理消息 ## 前置知识 <Spring AMQP 源码分析 04 - MessageListener> ## 相 ...
- lambda表达式/对象引用计数
★lambda表达式的用法例:I=[(lambda x: x*2),(lambda y: y*3)]调用:for x in I: print x(2)输出:4,6 ★获取对象的引用次数sys.getr ...
- Codeforces 895C - Square Subsets
895C - Square Subsets 思路:状压dp. 每个数最大到70,1到70有19个质数,给这19个质数标号,与状态中的每一位对应. 状压:一个数含有这个质因子奇数个,那么他状态的这一位是 ...
- robotframework安装与配置--学习第一天
刚刚入职公司,之前学的是Java+selenium自动化测试,但公司要求使用robot framework,所以找了些资料学习.刚开始觉得为什么不用java.python等开发语言+selenium做 ...
- Confluence 6 配置用户目录
一个用户目录是你存储你的用户和用户组信息的地方.用户信息包括有用户的全名,用户名,密码和电子邮件地址以及其他的一些个人信息. 用户组包括有用户组名字,属于这个用户组的用户和有可能属于这个用户组的另一个 ...
- Leetcode 114
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- SQL Server 创建定时任务JOB
1. SQL Server 代理 → 作业(右键)→新建作业 2.在[常规]中输入 “名称” 并确认已勾选 “已启动”. 3.在[步骤]中 "新建步骤": 填写 “步骤名称 ...
- fabric 学习笔记
fabric安装 目前,从PyPI可以搜索到主要的fabric库为“ Fabric 2.1.3 ”.“ fabric2 2.1.3 ”和“ Fabric3 1.14.post1 ”. Fabric:官 ...
- JavaScript学习总结(四)——逻辑OR运算符详解
在JavaScript中,逻辑OR运算符用||表示 1 var bTrue = true; 2 var bFalse = false; 3 var bResult = bTrue || bFalse; ...
- 小程序中的bindtap和catchtap的区别(交流QQ群:604788754)
bindtap绑定的节点,如果他的父节点也有绑定事件,点击之后就会出现冒泡. catchtap绑定的节点,如果他的父节点也有绑定事件,点击之后不会出现冒泡.