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:

  1. """
  2. # Definition for a Node.
  3. class Node(object):
  4. def __init__(self, val, children):
  5. self.val = val
  6. self.children = children
  7. """
  8. class Solution(object):
  9. def preorder(self, root):
  10. """
  11. :type root: Node
  12. :rtype: List[int]
  13. """
  14. l=[]
  15.  
  16. def subpreorderfun(r):
  17. if r:
  18. l.append(r.val)
  19. for c in r.children:
  20. subpreorderfun(c)
  21.  
  22. subpreorderfun(root)
  23.  
  24. return l

  

Iteration Method:

  1. """
  2. # Definition for a Node.
  3. class Node(object):
  4. def __init__(self, val, children):
  5. self.val = val
  6. self.children = children
  7. """
  8. class Solution(object):
  9. def preorder(self, root):
  10. """
  11. :type root: Node
  12. :rtype: List[int]
  13. """
  14. l=[]
  15. q=[root]
  16.  
  17. if root:
  18. p=[]
  19. while q:
  20. a=q.pop(0)
  21. l.append(a.val)
  22.  
  23. for c in a.children:
  24. p.append(c)
  25.  
  26. n=len(p)
  27. for i in range(n):
  28. q=[p.pop()]+q
  29.  
  30. 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. 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 ...

  2. Qt_Android_书

    1. http://bbs.csdn.net/topics/390942701 <<Qt on Android 核心编程>> Qt Quick核心编程 2.

  3. js中如何访问对象和数组

    js中如何访问对象和数组 一.总结 一句话总结:js访问对象点和中括号,访问数组的话就是中括号 对象 . [] 数组 [] 1.js访问对象的两种方式? . [] 可以使用下面两种方式访问对象的属性和 ...

  4. vs2010打包安装

    [WinForm] VS2010发布.打包安装程序(超全超详细) 2017年02月17日 21:47:09 y13156556538 阅读数:16487更多 个人分类: C#winform   1. ...

  5. javascritp文章 You-Dont-Know-JS

    https://github.com/getify/You-Dont-Know-JS 有6个系列.git在线免费. 第一本, up and going (点击链接) Mission: 作者建议在开始学 ...

  6. 在 Confluence 6 中连接一个 LDAP 目录

    希望将 Confluence 连接到一个 LDAP 目录: 在屏幕的右上角单击 控制台按钮 ,然后选择 General Configuration 链接. 在左侧的面板中单击 用户目录(User Di ...

  7. thinkphp3.2导出

    public function test() { set_time_limit(0); ini_set('memory_limit', '500M'); //导入PHPExcel类库,因为PHPExc ...

  8. linux 检查补丁包是否安装 名称 版本 release号

    To determine whether the required packages are installed, enter commands similar to the following: # ...

  9. UVALive 5881

    DES:给出一个数列.然后有q个询问,对给定的区间内是否有重复的数.如果有输出任意一个重复的.如果没有输出OK. 开始以为是线段树.后来发现.只是水的比较隐蔽.感觉这个方法还是很聪明的.把每个点的最近 ...

  10. learning uboot bootargs panic parameter

    By passing the kernel panic parameter, the system automatically resets after 3 seconds when kernel p ...