LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)
题目描述
给定一个二叉树,返回它的 后序 遍历。
示例:
输入: [1,null,2,3]
1
\
2
/
3 输出: [3,2,1]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
解题思路
后序遍历的顺序是左孩子->右孩子->父节点,对于每个遍历到的节点,执行如下操作:
- 首先对该节点不断沿左孩子方向向下遍历并入栈,直到左孩子为空
- 取出栈顶节点,此时该节点为树的最左下节点,若其右孩子不为空,则回到步骤1;若为空说明其为叶子节点,将其值输出到结果中,并记录pre为当前节点
- 在步骤2判断右孩子是否为空时,同时判断当前节点右孩子是否已被输出,如果pre是其右孩子,说明当前节点的所有子节点已访问过,所以不必再向下遍历,直接输出即可
代码
/**
* 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;
TreeNode* node = root, *pre = NULL;
stack<TreeNode*> s;
while(node || s.size()){
while(node){
s.push(node);
node = node->left;
}
node = s.top();
if(node->right && node->right != pre)
node = node->right;
else{
res.push_back(node->val);
s.pop();
pre = node;
node = NULL;
}
}
return res;
}
};
LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)的更多相关文章
- LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)
145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...
- [Swift]LeetCode145. 二叉树的后序遍历 | Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
- LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)
94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...
- Java实现 LeetCode 145 二叉树的后序遍历
145. 二叉树的后序遍历 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成 ...
- LeetCode 145 二叉树的后序遍历(非递归)
题目: 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路: 1 ...
- Leetcode 145. 二叉树的后序遍历
题目链接 https://leetcode-cn.com/problems/binary-tree-postorder-traversal/description/ 题目描述 给定一个二叉树,返回它的 ...
- LeetCode 145. 二叉树的后序遍历 (用栈实现后序遍历二叉树的非递归算法)
题目链接:https://leetcode-cn.com/problems/binary-tree-postorder-traversal/ 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [ ...
- LeetCode 145 ——二叉树的后序遍历
1. 题目 2. 解答 2.1. 递归法 定义一个存放树中数据的向量 data,从根节点开始,如果节点不为空,那么 递归得到其左子树的数据向量 temp,将 temp 合并到 data 中去 递归得到 ...
- 【leetcode 145. 二叉树的后序遍历】解题报告
前往二叉树的:前序,中序,后序 遍历算法 方法一:递归 vector<int> res; vector<int> postorderTraversal(TreeNode* ro ...
随机推荐
- Mycat1.6启动报NumberFormatException解决方案(server内存太大)
https://blog.csdn.net/lijieshare/article/details/84826280 2019-09-02 18:28:27,829 [ERROR][main] 2019 ...
- ffmpeg处理视频命令
一:视频添加图片水印 ffmpeg -i a.mp4 -vf "movie=a.jpg[watermark];[in][watermark] overlay=main_w-overlay_w ...
- mongodb启用auth,使用密码登录
更新操作: db.users.update({'currentVersion':3},{$set:{'currentVersion':5}}) 首先安装下载(略过) mongod 启动服务,有多重启动 ...
- another-redis-desktop-manager
brew cask install another-redis-desktop-manager
- 5、vim编辑器
1.什么是VIM? 理解为windows下面的文本编辑器,比如记事本,比如word文档 2.为什么要学? 因为在后面我们配置的服务,都需要人为修改配置,以便让程序按照我们修改后的指示运行. 1.修改配 ...
- 20、linux启动流程和救援模式
1.Linux启动流程 2.Linux运行级别 1.什么是运行级别,运行级别就是操作系统当前正在运行的功能级别 System V init运行级别 systemd目标名称 作用 0 runlevel0 ...
- mtd介绍
转:http://blog.csdn.net/lwj103862095/article/details/21545791 MTD,Memory Technology Device即内存技术设备 字符设 ...
- MySQL的分表与分区
MySQL分表分区是解决大数据量导致MySQL性能低下的两种方法. 什么是MySQL分表 从表面意思上看,MySQL分表就是将一个表分成多个表,数据和数据结构都有可能会变.MySQL分表分为垂直分表和 ...
- MySQL 关于索引的操作
-- 索引分类? 1.普通索引 2.唯一索引 3.全文索引 4.组合索引 普通索引:仅加速查询,最基本的索引,没有任何限制 唯一索引:加速查询 + 列值唯一(可以有null) 全文索引:仅适用于MyI ...
- MySQL中主键的选择与磁盘性能
偶然看到了“Fotolog: Scaling the World\'s Largest Photo Blogging Community”,才发现很多数据库的优化其实道理都很简单,至高境界是当你面对问 ...