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二叉树后序遍历的更多相关文章

  1. LeetCode:145_Binary Tree Postorder Traversal | 二叉树后序遍历 | Hard

    题目:Binary Tree Postorder Traversal 二叉树的后序遍历,题目要求是采用非递归的方式,这个在上数据结构的课时已经很清楚了,二叉树的非递归遍历不管采用何种方式,都需要用到栈 ...

  2. LeetCode OJ:Binary Tree Postorder Traversal(后序遍历二叉树)

    Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...

  3. [Leetcode] Binary tree inorder traversal二叉树中序遍历

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  4. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  5. [leetcode]94. Binary Tree Inorder Traversal二叉树中序遍历

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...

  6. 94 Binary Tree Inorder Traversal(二叉树中序遍历Medium)

    题目意思:二叉树中序遍历,结果存在vector<int>中 解题思路:迭代 迭代实现: /** * Definition for a binary tree node. * struct ...

  7. 144 Binary Tree Preorder Traversal(二叉树先序遍历Medium)

    题目意思:二叉树先序遍历,结果存在vector<int>中 解题思路:1.递归(题目中说用递归做没什么意义,我也就贴贴代码吧) 2.迭代 迭代实现: class Solution { pu ...

  8. leetcode Binary Tree Postorder Traversal 二叉树后续遍历

    先给出递归版本的实现方法,有时间再弄个循环版的.代码如下: /** * Definition for binary tree * struct TreeNode { * int val; * Tree ...

  9. C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)

    145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...

随机推荐

  1. Arduino-元件简介

    DS18B20温度传感器 DS18B20是DALLAS公司生产的一种常用的温度传感器,其具有体积小巧.硬件功耗低.抗干扰能力强.精准度高的特点.该传感器具有单总线通讯的能力,电压范围为3.0V~5.5 ...

  2. lunix安装

    https://www.cnblogs.com/wcwen1990/p/7630545.html

  3. VS2015 更改C++模式

    亲爱的小伙伴,有没有发现你们的VS2015装完以后和老江湖们用的不一样了,人家的界面打开是这样的 而你的界面打开是这样的 虽然看是只有一左一右的区别,但是内在确实有好多不一样. 想不想想老江湖一样,拥 ...

  4. eclipse格式化

    一.eclipse格式化的必要性 1.便于阅读 2.便于协作 二.eclipse格式化快捷键 ctrl shift + F

  5. C++字符串拼接和输入

    一 .char类型字符串以空字符结尾 1.以空字符结尾,空字符被写作\0,其ASCII码为0,用来标记字符串的结尾. char dog[4]={'a','b','c','d'}   //不是一个字符串 ...

  6. LeetCode 445——两数相加 II

    1. 题目 2. 解答 2.1 方法一 在 LeetCode 206--反转链表 和 LeetCode 2--两数相加 的基础上,先对两个链表进行反转,然后求出和后再进行反转即可. /** * Def ...

  7. 1.EOS源码编译运行

    目前网络上都是针对老版EOS2.0源码编译的文章,我在mac上参考这些文章编译,最后发现根本就不对,最新版本只需一条命令(./eosio_build.sh,依赖库会自动安装的)即可.我根据这些文章手动 ...

  8. oraclize预言机资料

    oraclize预言机资料 智能合约如何可信的与外部世界交互: https://blog.csdn.net/sportshark/article/details/77477842 国外一篇讲得很详细的 ...

  9. POJ 1228 Grandpa's Estate(凸包唯一性判断)

    Description Being the only living descendant of his grandfather, Kamran the Believer inherited all o ...

  10. 《学习OpenCV》课后习题解答6

    题目:(P104) 使用cvCmp()创建一个掩码.加载一个真实的图像.使用cvsplit()将图像分割成红,绿,蓝三个单通道图像. a.找到并显示绿图. b.克隆这个绿图两次(分别命名为clone1 ...