[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 ...
随机推荐
- Qt for Embedded Linux
1. Qt for Embedded Linux http://doc.qt.io/qt-5/embedded-linux.html 2. Installing Qt for Embedded Lin ...
- shuoj 418 丢史蒂芬妮(素数筛+sg函数)
丢史蒂芬妮 代码: #include<bits/stdc++.h> using namespace std; +; int SG[N][N]; bool S[N]; vector<i ...
- HDU 6130 Kolakoski
Kolakoski 思路: 从前往后扩展,前后构成映射关系. 代码: #include<bits/stdc++.h> using namespace std; #define ll lon ...
- Java基础九--抽象类
Java基础九--抽象类 一.抽象类介绍 /*抽象类:抽象:笼统,模糊,看不懂!不具体. 特点:1,方法只有声明没有实现时,该方法就是抽象方法,需要被abstract修饰. 抽象方法必须定义在抽象类中 ...
- memcached-1.4.20 主要启动流程笔记
以下笔记主要是关注tcp模式下memcached的启动过程. main() 设置信号处理函数为sig_handler() 初始化系统设置,保存在全局变量settings里面 settings_init ...
- Perfect Groups CodeForces - 980D
链接 题目大意: 定义一个问题: 求集合$S$的最小划分数,使得每个划分内任意两个元素积均为完全平方数. 给定$n$元素序列$a$, 对$a$的所有子区间, 求出上述问题的结果, 最后要求输出所有结果 ...
- python-day33--进程间通信(IPC)
方式:队列(推荐使用) 一.基本情况 1.可以往队列里放任意类型的数据 2. 队列:先进先出 3. q=Queue(3) #可以设置队列中最多可以进入多少个值,也可以不设置 q.put('first' ...
- c++中的引用详解
什么是引用? 引用是C++语言的一个特殊的数据类型描述,用于在程序的不同的部分使用两个以上的变量名指向同一块地址,使得对其中任何一个变量的操作实际上都是对同一地址单元进行的. 使用时的注意事项: 引用 ...
- cf188C(最大子段和&&思维)
C. Functions again time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- HDU-5001 Walk (概率DP)
Problem Description I used to think I could be anything, but now I know that I couldn't do anything. ...