【LeetCode】145. Binary Tree Postorder Traversal 解题报告 (C++&Python)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/binary-tree-postorder-traversal/
题目描述
Given a binary tree, return the postorder traversal of its nodes’ values.
Example:
Input: [1,null,2,3]
1
\
2
/
3
Output: [3,2,1]
Follow up: Recursive solution is trivial, could you do it iteratively?
题目大意
二叉树的后序遍历。
解题方法
递归
最常见最简单的方法就是DFS。先把左右子树递归完成,然后把根节点的值也放入结果中。
C++代码如下:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> res;
if (!root) return res;
vector<int> left = postorderTraversal(root->left);
vector<int> right = postorderTraversal(root->right);
res.insert(res.end(), left.begin(), left.end());
res.insert(res.end(), right.begin(), right.end());
res.push_back(root->val);
return res;
}
};
Python代码如下:
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def postorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
res = []
if not root: return res
if root.left:
res.extend(self.postorderTraversal(root.left))
if root.right:
res.extend(self.postorderTraversal(root.right))
res.append(root.val)
return res
迭代
题目中说了递归方法太琐碎,能不能用迭代方法解决呢?
由于后序遍历把根节点放到了最后,而我们在遍历的过程中,一定先获得到根节点,那么我们可以先倒序,然后再反转。
后序遍历:左->右->根
。
我们的做法:根->右->左
,然后再反转。
即,先把根节点放入栈中,然后把它左孩子、右孩子依次放入,这样我们下次对栈内的元素遍历得到的顺序就是从右向左的,对于栈中弹出的每个节点都是如此。
得到的顺序是根->右子树(节点全部入栈)->左子树
的遍历方式,最后需要加一个翻转即可得到想要的后序遍历。
C++代码如下:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> res;
if (!root) return res;
stack<TreeNode*> st;
st.push(root);
while (!st.empty()) {
TreeNode* node = st.top(); st.pop();
if (!node) continue;
res.push_back(node->val);
st.push(node->left);
st.push(node->right);
}
reverse(res.begin(), res.end());
return res;
}
};
日期
2019 年 9 月 20 日 —— 是选择中国互联网式加班?还是外企式养生?
【LeetCode】145. Binary Tree Postorder Traversal 解题报告 (C++&Python)的更多相关文章
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
- LeetCode: Binary Tree Postorder Traversal 解题报告
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
- LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)
翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...
- [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- 【LeetCode】144. Binary Tree Preorder Traversal 解题报告(Python&C++&Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- 【LeetCode】590. N-ary Tree Postorder Traversal 解题报告 (C++&Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 相似题目 参考资料 日期 题目地址:htt ...
- Java for LeetCode 145 Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- leetcode 145. Binary Tree Postorder Traversal ----- java
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
随机推荐
- 通过mac地址确认二层交换机某个端口下接的终端设备IP
正常来说,二层交换机主要是通过mac地址进行通信的,这就导致我们无法直接通过arp表来确认交换机端口下终端设备的IP: 但我们仍然能通过查找二层交换机端口下学习到的mac地址,然后通过对照三层核心交换 ...
- .NET SAAS 架构与设计 -SqlSugar ORM
1.数据库设计 常用的Saas分库分为2种类型的库 1.1 基础信息库 主要存组织架构 .权限.字典.用户等 公共信息 性能优化:因为基础信息库是共享的,所以我们可以使用 读写分离,或者二级缓存来进行 ...
- 使用GitHub Action进行打包并自动推送至OSS
GitHub Action 是 GitHub 于 2018 年 10 月推出的一个 CI\CD 服务. 官方文档:https://docs.github.com/cn/actions CI\CD 持续 ...
- Android editttext只能输入不能删除(选中后被软键盘遮住)
感谢https://www.dutycode.com/post-20.html: 解决方法:在布局外外嵌一层scrollview.
- 浅讲.Net 6 之 WebApplicationBuilder
介绍 .Net 6为我们带来的一种全新的引导程序启动的方式.与之前的拆分成Program.cs和Startup不同,整个引导启动代码都在Program.cs中. WebApplicationBuild ...
- Kafka入门教程(一)
转自:https://blog.csdn.net/yuan_xw/article/details/51210954 1 Kafka入门教程 1.1 消息队列(Message Queue) Messag ...
- 爬虫系列:连接网站与解析 HTML
这篇文章是爬虫系列第三期,讲解使用 Python 连接到网站,并使用 BeautifulSoup 解析 HTML 页面. 在 Python 中我们使用 requests 库来访问目标网站,使用 Bea ...
- Excel 数据验证:分类选择及输入限制
几个简单设置让你的数据不再出错 如何快速选择某一大类中的细分小类 多级菜单 注意:引用可以创建二级目录,但是引用前应先用公式定义名称,然后引用,引用只能在本sheet操作.
- Redis数据类型内部编码规则及优化方式
Redis的每个键值都是使用一个redisObject结构体保存的,redisObject的定义如下: typedef struct redisObject { unsigned type:4; un ...
- 【Linux】【Basis】文件系统
FHS:Filesystem Hierarchy Standard Web site: https://wiki.linuxfoundation.org/lsb/fhs http://www.path ...