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 ...
随机推荐
- 使用 “Unicode 字符集 ” 使用错误,应该使用 “使用多字节字符集”
“void ATL::CStringT<BaseType,StringTraits>::Format(const wchar_t *,...)”: 不能将参数 1 从“const char ...
- JavaScript中with不推荐使用,为什么总是出现在面试题中?
with的基本使用 尴尬的with关键字 一.with的基本使用 with是用来扩展语句作用域的,什么意思呢?先来看看语法和示例: 语法: with(expression){ statement } ...
- JS和JS是IE上JavaScript或JScript的缩写。
JS和JS是IE上JavaScript或JScript的缩写.javascript是所有浏览器的开放式标准脚本语言JScript是微软自己的开放式脚本语言标准,只有微软的IE浏览器遵循.JScript ...
- 微信小程序中button去除默认的边框
button { position:relative; display:block; margin-left:auto; margin-right:auto; padding-left:14px; p ...
- 编译原理-递归下降分析法 c程序部分的分析
实验三 语法分析程序实验 专业 商软2班 姓名 黄仲浩 学号 201506110166 一. 实验目的 编制一个部分文法分析程序. 二. 实验内容和要求 输入:源程序字符串 输出:正确 ...
- fastadmin 隐藏操作栏按钮
formatter: function (value, row, index) { var that = $.extend({}, this); $(table).data({"operat ...
- mybatis-04【小结】
mybatis-04[小结] 1.Mybatis 中 # 和 $ 的区别?#相当于对数据 加上 双引号,$相当于直接显示数据1)#将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号. 如:o ...
- 基础数据 补充 set() 集合 深浅拷贝
一 对字符串的操作 li = ["张曼玉", "朱茵", "关之琳", "刘嘉玲"] s = "_" ...
- 6.Shell 计划任务服务程序
计划任务服务程序 经验丰富的系统运维工程师可以使得Linux在无需人为介入的情况下,在指定的时间段自动启用或停止某些服务或命令,从而实现运维的自动化. 如何设置服务器的计划任务服务,把周期性.规律性的 ...
- uCos-II移值(二)
os_cpu_c.c文件 该文件主要是根据处理器平台特点完成任务堆栈初始化函数OSTaskStkInit以及其他几个用户Hook函数的编写,其中必须要实现的函数是OSTaskStkInit(在创建任务 ...