[Leetcode] Binary tree postorder traversal二叉树后序遍历
Given a binary tree, return the postorder traversal of its nodes' values.
For example:
Given binary tree{1,#,2,3},
1
\
2
/
3
return[3,2,1].
Note: Recursive solution is trivial, could you do it iteratively?
后序遍历:左孩子->右孩子->根节点
后序遍历最关键的是利用一个指针保存前一个访问过的信息,避免重复访问。
思路一:
左、右孩子是交叉入栈的,当出栈时,就存在如何从左孩子转向右孩子同时避免重复访问的问题。一个节点值被取出来时,它的左右子节点要么不存在,要么已经被访问过。所以,用head来保存上一个已访问元素的信息,然后判断是否为栈顶元素的左右孩子来实现避免重复访问,至于从左转向右,入栈的顺序已经解决这个问题。原代码
/**
* Definition for binary tree
* 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;
stack<TreeNode *>stk;
if(root) stk.push(root);
TreeNode *head=root;
while( !stk.empty())
{
TreeNode *top=stk.top();
if(( !top->left&& !top->right)||top->left==head||top->right==head)
{ //两种情况:一、到达叶节点;二、前一元素是栈顶的左、右孩子即被访问过
res.push_back(top->val);
stk.pop();
head=top;
}
else //结合栈的特点和后边遍历的顺序,右先进
{
if(top->right)
stk.push(top->right);
if(top->left)
stk.push(top->left);
}
}
return res;
}
};
思路二:
【后序遍历二叉树的步骤:先遍历二叉树的左子树,再遍历二叉树的右子树,最后访问根结点。非递归实现时,用一个栈模拟遍历过程。由于访问完左子树后访问右子树,栈中元素要起到转向访问其右子树的作用,但是不能像先序和中序遍历那样出栈即可,因为根结点时最后访问的。那么什么时候出栈呢?我们需要一个指针pre来记录前一次访问的结点。如果pre是根结点的右子树,则说明根结点的右子树访问完了,此时根结点就可以出栈了(源代码)】。过程:将左孩子不停的压入栈中,直到由root=root->left得到root为空,然后将栈顶元素的值存入res,用pre记录栈顶元素(在后来可以避免重复访问)。在下次循环中,因,root为空,所以跳过if进入判断else if实现左孩子向右孩子的转变。整体上,左孩子出栈以后,右孩子入栈,用pre记录右孩子,右孩子出栈,利用stk.top()->right !=pre来避免重复访问。
/**
* Definition for binary tree
* 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;
stack<TreeNode *> stk; TreeNode *pre=NULL;
while(root|| !stk.empty())
{
if(root)
{
stk.push(root);
root=root->left;
}
else if(stk.top()->right !=pre) //判断stk.top()->right是否被访问过
{
root=stk.top()->right;
pre=NULL;
}
else
{
res.push_back(stk.top()->val);
pre=stk.top();
stk.pop();
}
}
return res; }
};
思路三:递归版
class Solution {
public:
vector<int> postorderTraversal(TreeNode *root)
{
vector<int> res;
postOrderTrav(root,res);
return res;
}
void postOrderTrav(TreeNode *root,vector<int> &res)
{
if(root==NULL) return;
postOrderTrav(root->left,res);
postOrderTrav(root->right,res);
res.push_back(root->val);
}
};
[Leetcode] Binary tree postorder traversal二叉树后序遍历的更多相关文章
- LeetCode:145_Binary Tree Postorder Traversal | 二叉树后序遍历 | Hard
题目:Binary Tree Postorder Traversal 二叉树的后序遍历,题目要求是采用非递归的方式,这个在上数据结构的课时已经很清楚了,二叉树的非递归遍历不管采用何种方式,都需要用到栈 ...
- LeetCode OJ:Binary Tree Postorder Traversal(后序遍历二叉树)
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- [Leetcode] Binary tree inorder traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- [leetcode]94. Binary Tree Inorder Traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...
- 94 Binary Tree Inorder Traversal(二叉树中序遍历Medium)
题目意思:二叉树中序遍历,结果存在vector<int>中 解题思路:迭代 迭代实现: /** * Definition for a binary tree node. * struct ...
- 144 Binary Tree Preorder Traversal(二叉树先序遍历Medium)
题目意思:二叉树先序遍历,结果存在vector<int>中 解题思路:1.递归(题目中说用递归做没什么意义,我也就贴贴代码吧) 2.迭代 迭代实现: class Solution { pu ...
- leetcode Binary Tree Postorder Traversal 二叉树后续遍历
先给出递归版本的实现方法,有时间再弄个循环版的.代码如下: /** * Definition for binary tree * struct TreeNode { * int val; * Tree ...
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
随机推荐
- cookie的介绍和自动化中cookie的操作
1 cookie是什么? cookie: 1. Cookie是一小段的文本信息:格式:python中的字典(键值对组成) 2. Cookie产生:客户端请求服务器,如果服务器需要记录该用户状态,就向客 ...
- HDU - 6440(费马小定理)
链接:HDU - 6440 题意:重新定义加法和乘法,使得 (m+n)^p = m^p + n^p 成立,p是素数.,且satisfied that there exists an integer q ...
- 365. Count 1 in Binary【LintCode java】
Description Count how many 1 in binary representation of a 32-bit integer. Example Given 32, return ...
- [Install] TeamViewer
安装TeamViwer 1. $ sudo apt-get -f install 2. 使用gdebi安装TeamViwer. 所以先安装gdebi package. $ sudo apt-get i ...
- usb_modeswitch移植
usb_modeswitch移植 交叉工具链安装 交叉编译安装libsub库 交叉编译安装lib-compat-x.x.x 交叉编译安装usb_modeswitch 交叉编译工具链 为了使编译的程序可 ...
- IntelliJ IDEA 2017.3/2018.1/.2 激活
传统的License Server方式已经无法注册IntelliJ IDEA2017.3的版本了. http://idea.lanyus.com,这个网站有破解补丁和注册码两种方式,另外http:// ...
- 各类4G手机进入工参模式查看手机信息
随着移动4G正式商用,LTE网络建设日益完善,LTE用户日趋增多,通过进入其工程模式读取服务小区电平RSRP.物理小区标识PCI和频点号等基本信息的方式来判断测试点信号质量的优劣.由于市场上商用终端品 ...
- 20170413B端业务访问故障排查思路
现象: 1.全国用户电视端页面无法显示,刷不出版面. 2.后端服务无法打开,报错,504,502 显示服务器端业务故障超时. 3.其他业务也出现缓慢情况,并不严重. 排查: 1.系统服务排查,常规 ...
- 科普:PCI-E插槽都有哪些样子?
主板上的扩展插槽曾经是多种多样的,例如曾经非常流行的组合就是PCI插槽搭配AGP插槽,其中AGP插槽主要用在显卡上,而PCI插槽的用途则更广一些,不仅有用在显卡上,还能用于扩展其它设备,如网卡.声卡. ...
- Ubuntu录制gif动态图
大神写博客的时候通常一个Demo会附带一个动态图展示效果.在windows和mac上应该很容易找到录制工具,下面记录一下我在ubuntu下录制gif的过程. 下载byzanz录制工具 在ubuntu软 ...