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的更多相关文章

  1. 【Leetcode】【Medium】Binary Tree Preorder Traversal

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

  2. 【leetcode刷题笔记】Binary Tree Preorder Traversal

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

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

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

  5. [Leetcode 144]二叉树前序遍历Binary Tree Preorder Traversal

    [题目] Given a binary tree, return the preordertraversal of its nodes' values. Example: Input: [1,null ...

  6. LeetCode 589. N叉树的前序遍历(N-ary Tree Preorder Traversal)

    589. N叉树的前序遍历 589. N-ary Tree Preorder Traversal LeetCode589. N-ary Tree Preorder Traversal 题目描述 给定一 ...

  7. LeetCode 144. 二叉树的前序遍历(Binary Tree Preorder Traversal)

    144. 二叉树的前序遍历 144. Binary Tree Preorder Traversal 题目描述 给定一个二叉树,返回它的 前序 遍历. LeetCode144. Binary Tree ...

  8. 【leetcode_easy】589. N-ary Tree Preorder Traversal

    problem 589. N-ary Tree Preorder Traversal N叉树的前序遍历 首先复习一下树的4种遍历,前序.中序.后序和层序.其中,前序.中序和后序是根据根节点的顺序进行区 ...

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

随机推荐

  1. Python day14迭代器,三元表达式,列表解析以及生成器表达式

    1.迭代器 str=['sds','ccc','dw'] lit_1=str.__iter__()#获取迭代器 print(lit_1.__next__())#打印下一个值 # 用while做for的 ...

  2. c语言的左移和右移

    转自:https://www.cnblogs.com/myblesh/articles/2431806.html 先说左移,左移就是把一个数的所有位都向左移动若干位,在C中用<<运算符.例 ...

  3. GO 和 KEGG 的区别 | GO KEGG数据库用法 | 基因集功能注释 | 代谢通路富集

    一直都搞不清楚这两者的具体区别. 其实初学者搞不清楚很正常,因为它们的本质是相通的,都是对基因进行归类注释的数据库. 建议初学者自己使用一下这两个数据库,应该很快就能明白其中的区别. (抱歉之前没讲清 ...

  4. English trip -- VC(情景课) 7 A Shopping 购物

    Words The clothes place a dress 长裙      short skirt 短裙 pants 裤子   /  trousers 长裤  / shorts 短裤 a shir ...

  5. DOM文档对象模型

  6. Codeforces Round #449 (Div. 1)C - Willem, Chtholly and Seniorious

    ODT(主要特征就是推平一段区间) 其实就是用set来维护三元组,因为数据随机所以可以证明复杂度不超过O(NlogN),其他的都是暴力维护 主要操作是split,把区间分成两个,用lowerbound ...

  7. java 中利用异或实现两个变量互换

    一般实现两个变量之间的互换要用第三个变量,这样做可以,但创建新变量,增加了系统开销.如果要交换的变量时两个整数型变量,可以用更高效的方法.例如:^(异或)操作,举例如下: package chapte ...

  8. OC @property @synthesize和id

    文顶顶   OC语言@property @synthesize和id OC语言@property @synthesize和id 一.@property @synthesize关键字 注意:这两个关键字 ...

  9. Sql Server约束的学习一(主键约束、外键约束、唯一约束)

    一.约束的分类 1.实体约束 实体约束是关于行的,比如某一行出现的值不允许出现在其他行,例如主键约束. 2.域约束 域约束是关于列的,对于所有行,某一列有那些约束,例如检查约束. 3.参照完整性约束 ...

  10. bzoj1215

    题解: 暴力枚举每一种方案,然后hash判重 代码: #include<bits/stdc++.h> #define eps 1e-7 using namespace std; ],r[] ...