DFS,两种实现方式,递归和栈.

"""
# Definition for a Node.
class Node:
def __init__(self, val, children):
self.val = val
self.children = children
"""
class Solution:
def preorder(self, root: 'Node') -> List[int]:
if not root:
return []
stack,ans=[root],[]
while stack:
node=stack.pop()
ans.append(node.val)
if node.children:
stack.extend(reversed(node.children))
return ans
"""
# Definition for a Node.
class Node:
def __init__(self, val, children):
self.val = val
self.children = children
"""
class Solution:
def preorder(self, root: 'Node') -> List[int]:
if not root:
return []
ans=[root.val]
for c in root.children:
ans+=self.preorder(c)
return ans

Leetcode 589. N-ary Tree Preorder Traversal的更多相关文章

  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】144. Binary Tree Preorder Traversal 解题报告(Python&C++&Java)

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

  3. 【LeetCode】144. Binary Tree Preorder Traversal

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

  4. LeetCode OJ 144. Binary Tree Preorder Traversal

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

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

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

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

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

  7. 【LEETCODE OJ】Binary Tree Preorder Traversal

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

  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; ...

  10. LeetCode 589. N叉树的前序遍历(N-ary Tree Preorder Traversal)

    589. N叉树的前序遍历 589. N-ary Tree Preorder Traversal LeetCode589. N-ary Tree Preorder Traversal 题目描述 给定一 ...

随机推荐

  1. python 2 和python 3的 区别

    用户交互 input  ps:python2:raw_input python3:input 在 python2里 print不需要加括号也可以打印 子python3里 print 必须加括号才能打印

  2. ie9下面的console的bug

    摘自:http://blog.csdn.net/cdnight/article/details/51094464 ie9下面,很奇怪的是有console的代码有时候执行不下去,不过当f12打开控制台的 ...

  3. linux 文本分析工具---awk命令(7/1)

    awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各 ...

  4. Connection.setAutoCommit使用的注意事项

    http://blog.csdn.net/xiayimiaokuaile/article/details/6422032 setAutoCommit总的来说就是保持数据的完整性,一个系统的更新操作可能 ...

  5. 【HTML5校企公益课】第四天

    1.上午考试没去.. 2.下午跟不上.. 变色.html <!DOCTYPE html> <html> <head> <meta charset=" ...

  6. Linux关于yum命令Error: Cannot retrieve repository metadata (repomd.xml) for repository:xxxxxx.

    Linux关于yum命令Error: Cannot retrieve repository metadata (repomd.xml) for repository:xxxxxx. 问题: Linux ...

  7. windows10下Python如何设置环境变量

    1.右击“我的电脑”,选择“属性”, 2.选择“高级系统设置”, 3.选择“环境变量”, 4.在“系统变量”中选中“Path”,再点“新建”.(Python.Scripts两个目录都要加,只加Pyth ...

  8. 20145109 《Java程序设计》第三周学习总结

    20145109 <Java程序设计>第三周学习总结 教材学习内容总结 Chapter 4 Object 4.1 Class & Object definition of clas ...

  9. ASP.NET的内置对象

    Request   该对象用于检索从浏览器向服务器所发送的请求中的信息.在按下“提交”按钮时,Request对象将读取和提取通过HTTP请求发送的参数.在用户提交表单时,包含在输入控件中的数据将与表单 ...

  10. Spring Boot 中使用jsp

    接SpringBoot 快速入门(Eclipse): 步骤一:视图支持 Springboot的默认视图支持是Thymeleaf,但是Thymeleaf我们不熟悉,我们熟悉的还是jsp. 所以下面是讲解 ...