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

  1. 【LeetCode】144. Binary Tree Preorder Traversal (3 solutions)

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  2. LeetCode算法题-N-ary Tree Preorder Traversal(Java实现)

    这是悦乐书的第268次更新,第282篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第135题(顺位题号是589).给定一个n-ary树,返回其节点值的前序遍历.例如,给定 ...

  3. 【LeetCode】144. Binary Tree Preorder Traversal 解题报告(Python&C++&Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

  4. 【LEETCODE OJ】Binary Tree Preorder Traversal

    Problem Link: http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ Even iterative solutio ...

  5. 【LeetCode】144. Binary Tree Preorder Traversal

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

  6. LeetCode OJ 144. Binary Tree Preorder Traversal

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

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

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

  8. LeetCode题解之N-ary Tree Preorder Traversal

    1.题目描述 2.问题分析 采用递归方法是标准解法. 3.代码 vector<int> preorder(Node* root) { vector<int> v; preNor ...

  9. LeetCode题解之 Binary Tree Preorder Traversal

    1.题目描述 2.问题分析 利用递归. 3.代码 vector<int> preorderTraversal(TreeNode* root) { vector<int> v; ...

随机推荐

  1. 【WEB前端开发最佳实践系列】JavaScript篇

    一.养成良好的编码习惯,提高可维护性 1.避免定义全局变量和函数,解决全局变量而导致的代码“污染”最简单的额方法就是把变量和方法封装在一个变量对象上,使其变成对象的属性: var myCurrentA ...

  2. 【linux系列】配置免密登陆

    一.SSH无密码登录原理 此操作是为了搭建hadoop集群进行的操作 Master(NameNode|JobTracker)作为客户端,要实现无密码公钥认证,连接到服务器Salve(DataNode| ...

  3. 使用TELNET手工操作 IMAP 查看邮件

    http://www.cnblogs.com/CrazyWill/archive/2006/08/12/474884.html IMAP 协议收信与POP收信有很大的不同,最明显的一点就是发送的每条命 ...

  4. jpa中时间戳格式应该用哪种类型

    遇到个bug,数据库时间存储用了datetime,但是下面的java jpa代码,查询回来,却只有日期. String innerSql = getInnerQuery(departmentId, k ...

  5. 记录一下使用Ubuntu16.0.4配置和使用docker registry

    h1, h2, h3, h4, h5, h6, p, blockquote { margin: 5px; padding: 5; } body { font-family: "Helveti ...

  6. Pexpect学习:

    pexecpt run用法:格式:run(command,timeout=-1,withexitstatus=False,events=None,extra_args=None,logfile=Non ...

  7. Mysql带返回值与不带返回值的2种存储过程

    过程1:带返回值: 1 drop procedure if exists proc_addNum; 2 create procedure proc_addNum (in x int,in y int, ...

  8. thinkphp---模糊查询详解

    最近做项目,在做搜索模块的时候,模糊查询肯定少不了. 今天就详细的看一下模糊查询: $where['title'] = array('like','%'.$words.'%'); $where['ti ...

  9. 第一次php之旅

    话说起来,我也是刚接触php不久,刚开始是因为想自己做一个从前端到后台完整的网站,所以去学后台技术,在各种语言的选择中,由于php语言的简单,易学,功能强大,开发速度快等原因,最终我选择了php! 一 ...

  10. window.onload的一些说明

    window.onload事件对于初学者来说,经常会让我们感觉不好理解,并且经常会犯一些错误,初学js的时候经常碰到有关于它的问题,我想和我一样很多初学者也会碰到,那时候不懂它的具体作用,只要一写代码 ...