【LeetCode】590. N-ary Tree Postorder Traversal 解题报告 (C++&Python)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/n-ary-tree-postorder-traversal/description/
题目描述
Given an n-ary tree, return the postorder traversal of its nodes’ values.
For example, given a 3-ary tree:
Return its postorder traversal as: [5,6,3,2,4,1].
Note: Recursive solution is trivial, could you do it iteratively?
题目大意
N叉树的后序遍历。
解题方法
递归
首先得明白,这个N叉树是什么样的数据结构定义的。val是节点的值,children是一个列表,这个列表保存了其所有节点。
后序遍历,如果通过递归还是非常简单的。对其子节点遍历,在对其本身节点遍历即可。由于所有的子节点是个列表,这样甚至比二叉树还要简单,只需对列表进行循环就行了。
Python代码如下:
"""
# Definition for a Node.
class Node(object):
def __init__(self, val, children):
self.val = val
self.children = children
"""
class Solution(object):
def postorder(self, root):
"""
:type root: Node
:rtype: List[int]
"""
res = []
if not root:
return res
for child in root.children:
res.extend(self.postorder(child))
res.append(root.val)
return res
C++代码如下:
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children;
Node() {}
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
vector<int> postorder(Node* root) {
vector<int> res;
if (!root) return res;
for (Node* child : root->children) {
vector<int> child_vector = postorder(child);
res.insert(res.end(), child_vector.begin(), child_vector.end());
}
res.push_back(root->val);
return res;
}
};
迭代
这个题希望我们使用迭代方法去做,即使用迭代的方法得到后序遍历。由于后序遍历把根节点放到了最后,而我们在遍历的过程中,一定先获得到根节点,那么我们可以先倒序,然后再反转。
后序遍历:左->右->根
。
我们的做法:根->右->左
,然后再反转。
即,先把根节点放入栈中,然后把它的孩子从左到右依次放入,这样我们下次对栈内的元素遍历得到的顺序就是从右向左的,对于栈中弹出的每个节点都是如此。
得到的顺序是根->右子树(节点全部入栈)->左子树
的遍历方式,最后需要加一个翻转即可得到想要的后序遍历。
Python代码如下:
"""
# Definition for a Node.
class Node(object):
def __init__(self, val, children):
self.val = val
self.children = children
"""
class Solution(object):
def postorder(self, root):
"""
:type root: Node
:rtype: List[int]
"""
res = []
if not root:
return res
stack = [root,]
while stack:
node = stack.pop()
stack.extend(node.children)
res.append(node.val)
return res[::-1]
C++代码如下:
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children;
Node() {}
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
vector<int> postorder(Node* root) {
vector<int> res;
if (!root) return res;
stack<Node*> st;
st.push(root);
while (!st.empty()) {
Node* node = st.top(); st.pop();
if (!node) continue;
for (Node* child : node->children) {
st.push(child);
}
res.push_back(node->val);
}
reverse(res.begin(), res.end());
return res;
}
};
相似题目
https://blog.csdn.net/fuxuemingzhu/article/details/81021950
参考资料
https://leetcode.com/articles/n-ary-tree-postorder-transversal/
日期
2018 年 7 月 12 日 —— 天阴阴地潮潮,已经连着两天这样了
2018 年 11 月 5 日 —— 打了羽毛球,有点累
【LeetCode】590. N-ary Tree Postorder Traversal 解题报告 (C++&Python)的更多相关文章
- 【LeetCode】145. Binary Tree Postorder Traversal 解题报告 (C++&Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- 【LeetCode】144. Binary Tree Preorder Traversal 解题报告(Python&C++&Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- LeetCode: Binary Tree Postorder Traversal 解题报告
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- LeetCode 590 N-ary Tree Postorder Traversal 解题报告
题目要求 Given an n-ary tree, return the postorder traversal of its nodes' values. 题目分析及思路 题目给出一棵N叉树,要求返 ...
- 【LeetCode】889. Construct Binary Tree from Preorder and Postorder Traversal 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...
- 【LeetCode】589. N-ary Tree Preorder Traversal 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...
- 【LeetCode】449. Serialize and Deserialize BST 解题报告(Python)
[LeetCode]449. Serialize and Deserialize BST 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/pro ...
随机推荐
- 进阶版的java面试
来自一名2019届应届毕业生总结的Java研发面试题汇总(2019秋招篇) 2018年Java研发工程师面试题 Java研发工程师面试题(Java基础) ...
- 零基础学习java------37---------mybatis的高级映射(单表查询,多表(一对一,一对多)),逆向工程,Spring(IOC,DI,创建对象,AOP)
一. mybatis的高级映射 1 单表,字段不一致 resultType输出映射: 要求查询的字段名(数据库中表格的字段)和对应的java类型的属性名一致,数据可以完成封装映射 如果字段和jav ...
- 调试器gdb
1.启动和退出gdb gdb调试的对象是可执行文件,而不是程序源代码.如果要使一个可执行文件可以被gdb调试,那么在使用编译器gcc编译程序时加入-g选项.-g选项告诉gcc在编译程序时加入调试信息, ...
- 自定义控件CustomAlertView
[记录][完整代码最下] 效果如下: 可行性分析: 由于系统自带的UIAlertView样式简单,只有两种样式,想要理想的样式就要自定义控件了 文件名取为:CustomAlertView 创建文件如下 ...
- 如何在Swagger2或Swagger3中增加Json Web Token
1. 前言 Swagger 3.0已经发布有一段时间了,作为一个非常有用的文档工具已经越来越多的项目在使用它.而JWT也是目前前后端分离最常用的安全技术.那么如何在Swagger 3.0 中添加JWT ...
- 用户名、密码、整数等常用的js正则表达式
1 用户名正则 //用户名正则,4到16位(字母,数字,下划线,减号) var uPattern = /^[a-zA-Z0-9_-]{4,16}$/; //输出 true console.log(uP ...
- Linux学习 - 目录表
目录名 作用 权限 说明 /bin/ 存放系统命令的目录 所有用户 存放在/bin/下的命令单用户模式下也可以执行 /sbin/ 保存和系统环境设置相关的命令 root ...
- Python实战之MySQL数据库操作
1. 要想使Python可以操作MySQL数据库,首先需要安装MySQL-python包,在CentOS上可以使用一下命令来安装 $ sudo yum install MySQL-python 2. ...
- 【Linux】【Shell】【Basic】文件查找locate,find
1.locate: 1.1. 简介:依赖于事先构建好的索引库: 系统自动实现(周期性任务): 手动更新数据库(updatedb): 1.2. 工作特性:查找速度快:模糊 ...
- shiro免认证的路径配置
– ?:匹配一个字符,如/admin? 将匹配/admin1,但不匹配/admin 或/admin/:– *:匹配零个或多个字符串,如/admin 将匹配/admin./admin123,但不匹配/a ...