[LeetCode] 589. N-ary Tree Preorder Traversal_Easy
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].
这个题目思路就是跟LeetCode questions conlusion_InOrder, PreOrder, PostOrder traversal类似, recursive 和iterable都可以.
04/17/2019 update: 加第三种方法, 利用divide and conquer
1. Recursively
class Solution:
def nary_Preorder(self, root):
def helper(root):
if not root: return
ans.append(root.val)
for each in root.children:
helper(each)
ans = []
helper(root)
return ans
2. Iterable
class Solution:
def nary_preOrder(self, root):
if not root: return []
stack, ans = [root], []
while stack:
node = stack.pop()
ans.append(node.val)
for each in node.children[::-1]:
stack.append(each)
return ans
3. Divide and conquer
# Divide and conquer
class Solution:
def preOrder(self, root):
if not root: return []
temp = []
for each in root.children:
temp += self.preOrder(each)
return [root.val] + temp
[LeetCode] 589. N-ary Tree Preorder Traversal_Easy的更多相关文章
- 【LeetCode】144. Binary Tree Preorder Traversal (3 solutions)
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- LeetCode算法题-N-ary Tree Preorder Traversal(Java实现)
这是悦乐书的第268次更新,第282篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第135题(顺位题号是589).给定一个n-ary树,返回其节点值的前序遍历.例如,给定 ...
- 【LeetCode】144. Binary Tree Preorder Traversal 解题报告(Python&C++&Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- 【LEETCODE OJ】Binary Tree Preorder Traversal
Problem Link: http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ Even iterative solutio ...
- 【LeetCode】144. Binary Tree Preorder Traversal
题目: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binar ...
- LeetCode OJ 144. Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- LeetCode OJ:Binary Tree Preorder Traversal(前序遍历二叉树)
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- LeetCode题解之N-ary Tree Preorder Traversal
1.题目描述 2.问题分析 采用递归方法是标准解法. 3.代码 vector<int> preorder(Node* root) { vector<int> v; preNor ...
- LeetCode题解之 Binary Tree Preorder Traversal
1.题目描述 2.问题分析 利用递归. 3.代码 vector<int> preorderTraversal(TreeNode* root) { vector<int> v; ...
随机推荐
- 【linux系列】centos安装vsftp
一.检查vsftpd软件 如果发现上不了网可以修改配置文件中的ONBOOT=no改为yes,然后重启服务试试
- 原生js--客户端存储的种类
客户端存储遵循同源策略,不同的站点页面之间不可以相互读取对方的数据,但同一站点的不同页面之间可以共享存储的数据 客户端存储的种类: 1.web存储 localStorage.sessionStorag ...
- [原]rpm安装rpm-package报错:Header signature NOKEY 和 error: Failed dependencies:
以前经常遇到这个问题,一直未有记录,今天记录下来: 在安装rpm包的时候报错误如下: Question 1: warning: *.rpm: Header V3 DSA signature: NOKE ...
- [原]sublime Text2
sublime Text2 升级到 2.0.2 build 2221 64位 的破破解 sublime Text2 download website 链接: http://pan.baidu.com/ ...
- mongodb gridfs基本使用
Mongodb GridFS图片文件存储解决方案 之前解决方案是接收图片数据后,将图片直接存储到盘阵,然后通过Apache做服务器,将图片信息存储到数据库,并且存储一个Apache的访问路径. 目前需 ...
- 将Linux系统的字体全改成中文
# 修改字符集,否则可能报 input/output error的问题,因为日志里打印了中文 $ localedef -c -f UTF-8 -i zh_CN zh_CN.UTF-8 $ export ...
- MassTransit
http://stackoverflow.com/questions/15485317/newbie-is-a-consumer-queue-necessary-in-order-for-publis ...
- 提取ipa里面的资源图片
有时候发现个不错的UI,就想扒出来看看,ipa里的图片你知道的,都不能直接用,所以找到两个方法:一.用python转换1.将ipa文件后缀改为zip,然后解压缩.2.新建一个文件夹,将解压后的包里的p ...
- node项目部署相关问题
process.env process.env属性返回一个对象,包含了当前Shell的所有环境变量. 通常的做法是,新建一个环境变量NODE_ENV,用它确定当前所处的开发阶段,生产阶段设为produ ...
- ElasticSearch 安装 go-mysql-elasticsearch 同步mysql的数据
一.首先在Centos6.5上安装 go 语言环境 下载Golang语言包:https://studygolang.com/dl [hoojjack@localhost src]$ ls apache ...