[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; ...
随机推荐
- 【Phalapi】新加Namespace (模块)
官网地址: https://www.phalapi.net/ github 地址: https://github.com/phalapi/phalapi/tree/master-2x 1 compos ...
- storm事务
1. storm 事务 对于容错机制,Storm通过一个系统级别的组件acker,结合xor校验机制判断一个msg是否发送成功,进而spout可以重发该msg,保证一个msg在出错的情况下至少被重发一 ...
- ESlint全局变量报错
场景: 在main.js下申明了全局变量: /* eslint no-undef: "error" */ window.vm = new Vue({ el: '#app', rou ...
- css笔记——文本样式
聊聊text-decoration.text-indent.text-transform.letter-spacing.word-spacing.vertical-align.下面是一些常用设置文本样 ...
- C语言程序设计--字符串与指针及数组与指针
数组的基本知识 数组的定义 #define SIZE 5 int array_int[5]; //未声明初始化,默认填零 float array_float[5] = {1.01, 2.23, 3.1 ...
- C语言位操作--判断整数的符号
关于衡量计算操作的方法: 当为算法统计操作的数量的时候,所有的C运算符被认为是一样的操作.中间过程不被写入随机存储器(RAM)而不被计算,当然,这种操作数的计算方法,只是作为那些接近机器指令和CPU运 ...
- 23种设计模式之桥接模式(Bridge)
桥接模式将抽象部分与它的实现部分分离,使它们都可以独立地变化.它是一种对象结构型模式,又称为柄体(Handle and Body)模式或接口(Interface)模式.桥接模式类似于多重继承方案,但是 ...
- 【转】python中json.loads与eval的区别
JSON有两种结构: “名称/值”对的集合(A collection of name/value pairs).不同的语言中,它被理解为对象(object),纪录(record),结构(struct) ...
- jfinal的model和record如何相互转化?
一.model转record: Model类: 1. /** * Convert model to record. */public Record toRecord() { return new Re ...
- Shell输入输出重定向
全部可用的重定向命令列表 命令 说明 command > file 将输出重定向到 file. command < file 将输入重定向到 file. command >> ...