leetcode589. N-ary Tree Preorder Traversal
python 版:
class Solution(object):
def preorder(self, root):
ret, q = [], root and [root]
while q:
node = q.pop()
ret.append(node.val)
q += [child for child in node.children[::-1] if child]
return ret
看了,很久才看懂,因为太简洁,举一个小栗子会更好懂。


python关于构建树的数据结构:https://www.cnblogs.com/bjwu/p/9016566.html
自己写,反思疑问:
1.结点的容器定义,只有child的,没有左右节点,这个怎么确定是左边的叶子节点,还是右边的的呢?
2.这里面节点是如何存储的呢?看不懂。

答案如图,对容器和vector理解还不是很深入,现在记住了:vector就是不限长度的数组,而且这个数组可以放入任意类型的数据。

class Solution {
public:
void traversal(Node* root, vector<int>&ans)
{
ans.push_back(root->val);
for(auto i:root->children) traversal(i,ans);
}
vector<int> preorder(Node* root) {
vector<int> ans ;
if(root==NULL) return ans;
traversal(root,ans);
return ans;
}
};
其中利用 for 构造 迭代的方法值得借鉴。
leetcode589. N-ary Tree Preorder Traversal的更多相关文章
- LeetCode 589. N叉树的前序遍历(N-ary Tree Preorder Traversal)
589. N叉树的前序遍历 589. N-ary Tree Preorder Traversal LeetCode589. N-ary Tree Preorder Traversal 题目描述 给定一 ...
- 【LeetCode】Binary Tree Preorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal
详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal Given a binary tree, return the po ...
- 3月3日(3) Binary Tree Preorder Traversal
原题 Binary Tree Preorder Traversal 没什么好说的... 二叉树的前序遍历,当然如果我一样忘记了什么是前序遍历的.. 啊啊.. 总之,前序.中序.后序,是按照根的位置来 ...
- Binary Tree Preorder Traversal on LeetCode in Java
二叉树的非递归前序遍历,大抵是很多人信手拈来.不屑一顾的题目罢.然而因为本人记性不好.基础太差的缘故,做这道题的时候居然自己琢磨出了一种解法,虽然谈不上创新,但简单一搜也未发现雷同,权且记录,希望于人 ...
- Binary Tree Preorder Traversal and Binary Tree Postorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- [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 ...
- C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)
144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...
- LeetCode:144_Binary Tree Preorder Traversal | 二叉树的前序遍历 | Medium
题目:Binary Tree Preorder Traversal 二叉树的前序遍历,同样使用栈来解,代码如下: struct TreeNode { int val; TreeNode* left; ...
- 【LeetCode】144. Binary Tree Preorder Traversal (3 solutions)
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
随机推荐
- php返回数组后处理(开户成功后弹窗提示)
1. 在注册的时候,注册成功后经常会弹窗提示自己注册的信息,这类做法需要返回mysql数据库中获取的数组值,返回给前台页面,赋值给弹窗. 2.做法: 返回数组 打印的数组的值 返回数组处理 赋值给弹窗 ...
- android chrome iframe设置src属性无法启动app
0x01 Android Intents with Chrome Android有一个很少人知道的特性可以通过web页面发送intent来启动apps.以前通过网页启动app是通过设置iframe的s ...
- apply、call、bind有什么区别?
使用 apply var a = { name : "Cherry", func1: function () { console.log(this.name) }, func2: ...
- 代码实现自定义TableView
实现效果(通过代码的方式实现TableCell 的创建) 实现过程: 实现过程两个部分 1 数据源的准备 本例子采用NSDictionary +NSArray 为数据源 (接口部分) (数据初始化部分 ...
- php_soap扩展应用
WebServices简介 先给出一个概念 SOA ,即Service Oriented Architecture ,中文一般理解为面向服务的架构, 既然说是一种架构的话,所以一般认为 SOA 是包含 ...
- angularjs初识ng-app、ng-model、ng-repeat指令
ng-app属性是angular.js的标志语句,它标记了angular.js的作用域.ng-app可以添加在很多地方,像上面那样添加到html标签上,说明angular脚本对整个页面都起作用.也可以 ...
- 第一章 数据库和SQL
1-1 数据库是什么? 一.数据库的含义 数据库是将大量数据保存起来,通过计算机加工而成的可以进行高效访问的数据集合. 数据库DB 二.数据库管理系统 DBMS 用来管理数据库的计算机系统称为 ...
- MySql EF6 DBFirst 向导无法生成 edmx 解决方法(同:您的项目引用了最新实体框架;但是,找不到数据链接所需的与版本兼容的实体框架数据库提供程序)
使用 MySql EF6 DBfirst 生成模型时经常会遇到EF6模式无法选择的情况究其原因, 还是因为没有正确的使用 Connector/Net. 下面说一下使用方法. 使用 MySql DBFi ...
- [翻译] ValueTrackingSlider
ValueTrackingSlider What is it? A UISlider Subclass that displays live values in a popUpView. It’s i ...
- Jenkins定时构建和轮询SCM设置说明
看图说事: 一.定时构建:不管SVN或Git中数据有无变化,均执行定时化的构建任务 : 二.轮询SCM:只要SVN或Git中数据有更新,则执行构建任务: 三.构建语法说明: 1.首先格式为:* * * ...