[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 ...
随机推荐
- Windows下使用pip安装python包是报错-UnicodeDecodeError: 'ascii' codec can't decode byte 0xcb in position 0
先交待下开发环境: 操作系统:Windows 7 Python版本:2.7.9 Pip版本:6.1.1 其他环境忽略 在windows下使用pip下载python包,出现如下错误 Collecting ...
- Qt_Android_书
1. http://bbs.csdn.net/topics/390942701 <<Qt on Android 核心编程>> Qt Quick核心编程 2.
- js中如何访问对象和数组
js中如何访问对象和数组 一.总结 一句话总结:js访问对象点和中括号,访问数组的话就是中括号 对象 . [] 数组 [] 1.js访问对象的两种方式? . [] 可以使用下面两种方式访问对象的属性和 ...
- vs2010打包安装
[WinForm] VS2010发布.打包安装程序(超全超详细) 2017年02月17日 21:47:09 y13156556538 阅读数:16487更多 个人分类: C#winform 1. ...
- javascritp文章 You-Dont-Know-JS
https://github.com/getify/You-Dont-Know-JS 有6个系列.git在线免费. 第一本, up and going (点击链接) Mission: 作者建议在开始学 ...
- 在 Confluence 6 中连接一个 LDAP 目录
希望将 Confluence 连接到一个 LDAP 目录: 在屏幕的右上角单击 控制台按钮 ,然后选择 General Configuration 链接. 在左侧的面板中单击 用户目录(User Di ...
- thinkphp3.2导出
public function test() { set_time_limit(0); ini_set('memory_limit', '500M'); //导入PHPExcel类库,因为PHPExc ...
- linux 检查补丁包是否安装 名称 版本 release号
To determine whether the required packages are installed, enter commands similar to the following: # ...
- UVALive 5881
DES:给出一个数列.然后有q个询问,对给定的区间内是否有重复的数.如果有输出任意一个重复的.如果没有输出OK. 开始以为是线段树.后来发现.只是水的比较隐蔽.感觉这个方法还是很聪明的.把每个点的最近 ...
- learning uboot bootargs panic parameter
By passing the kernel panic parameter, the system automatically resets after 3 seconds when kernel p ...