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 ...
随机推荐
- Code Signal_练习题_adjacentElementsProduct
Given an array of integers, find the pair of adjacent elements that has the largest product and retu ...
- Python3 循环语句
Python3 循环语句 转来的 很适合小白 感谢作者 Python中的循环语句有 for 和 while. Python循环语句的控制结构图如下所示: while 循环 Python中wh ...
- linux 用户管理 groupadd、groupmod、groupdel、gpasswd
添加用户组groupadd [选项] 组名 /usr/sbin/groupadd执行权限:root一个用户可以属于多个所属组,但有一个缺省组,和用户名同名-g GID:指定组ID 修改用户组 grou ...
- 二、vue学习--父元素如何获取子元素的值,子元素如何获取父元素的值
下图是父元素: 下图是子元素,获取父元素的值,使用props定义属性,这样就可以获取到父元素上传过来的set .place.type,拿到值就可以做一些自己的逻辑处理 二.子元素给父元素传值? 下 ...
- 解决升级PHP7.1后,发邮件时提示“fsockopen(): Peer certificate CN=`xxx.xx.com' did not match expected CN=`113.x.xx.98”
把项目环境升级到PHP7.1后,发现在不使用SSL时可以使用IP发邮件,可设置成SSL时就只能使用hostname发送,PHP提示的错误信息大致意思是说,IP与hostname无法通过SSL验证,修改 ...
- 用jQuery Validate+layer插件实现好看的表单提交效果
作为初学者,以前做表单验证都是自己写的,目的是让自己更好的了解代码,加深自己对javascript的理解,但是其实在很久都知道有一个很好用的表单验证插件:jQuery Validate.js,一直都没 ...
- 使用jqGrid过程中出现的问题
在使用jqGrid过程中,需要后台查询数据添加到表格中,在js中循环调用addRowData方法时出现浏览器崩溃现象. 原因:jqGrid的addRowData方法中做了一系列的处理,在后台返回数据量 ...
- nginx安装脚本
#!/bin/bash#auto config Nginx#by zhangjia 2019#define Path variables#date:2019/1/16 check_ok() { ]] ...
- 第八章 计时器(BEEPER2)
/*------------------------------------- BEEPER2.C -- Timer Demo Program No.1 (c) Charles Petzold, 19 ...
- Scala编程之访问修饰符
private ,protected,public,在不加前两者声明时为public为公共式访问: private为私有式访问:protected为家族式访问,与Java一致. object Oute ...