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 ...
随机推荐
- 前端日报-20160527 underscore 源码解读
underscore 源码解读 API文档浏览器 JavaScript 中加号操作符细节 抛弃 jQuery,拥抱原生 JS 从 0 开始学习 GitHub 系列之「加入 GitHub」 js实现克隆 ...
- Drupal8学习之路--官网文档碎碎记--主题篇
主要记录一些琐碎的知识点. 1.“In Drupal 8 drupal_add_css(), drupal_add_js() and drupal_add_library()were removed ...
- 【MUI框架】学习笔记整理 Day 2
参考整理自MUI官网 http://dev.dcloud.net.cn/mui/ui/ (1)numbox(数字输入框) mui提供了数字输入框控件,可直接输入数字,也可以点击“+”.“-”按钮变换当 ...
- JavaScript--浅谈DOM操作
JavaScript之浅谈DOM操作 1.理解DOM: DOM(Document Object Model ,文档对象模型)一种独立于语言,用于操作xml,html文档的应用编程接口. 怎么说,我从两 ...
- 这几个Xocode插件用过一段时间还比较稳定好用,Xcode6兼容,推荐给大家:
这几个Xocode插件用过一段时间还比较稳定好用,Xcode6兼容,推荐给大家: AdjustFontSize: 快捷调整Xcode字体,https://github.com/zats/AdjustF ...
- struts2 开发模式
在struts.xml中增加: <constant name="struts.devMode" value="true" />
- 【转】Twitter Storm如何保证消息不丢失
Twitter Storm如何保证消息不丢失 发表于 2011 年 09 月 30 日 由 xumingming 作者: xumingming | 可以转载, 但必须以超链接形式标明文章原始出处和作者 ...
- centos7.2+php7.2+nginx1.12.0+mysql5.7配置
一. 源码安装php7.2 选择需要的php版本 从 php官网: http://cn2.php.net/downloads.php 选择需要的php版本,选择.tar.gz 的下载包,点击进入,选择 ...
- webpack-易混淆部分的解释
原文链接: https://medium.com/@rajaraodv/webpack-the-confusing-parts-58712f8fcad9 webpack的核心哲学 1. 任何皆模块 正 ...
- Vue2学习笔记:html属性
1.使用 <!DOCTYPE html> <html> <head> <title></title> <meta charset=&q ...