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].

解题:

二叉树的后序遍历,比先序和中序遍历稍显麻烦一些,有兴趣可查看:二叉树先序遍历二叉树中序遍历

后序遍历需要输出左子树后,输出右子树,最后输出当前结点。复杂的原因是,输出左右子树后,如何判断输出的结点是左还是右。如果左结点已经输出,就继续输出右;如果右结点已经输出,就输出当前结点,并pop出栈中上一层的结点继续考察。因此,后序遍历和先中序遍历代码上主要不同是:

1、引入lastNode指针,指向刚刚输出的结点,方便判断刚刚输出的结点是上一层的左儿子还是右儿子;

2、如果一个结点,连同其左子树和右子树都已经输出完毕,应继续pop栈中元素,但是新pop出的结点,在下一次循环中不应再循环遍历其左子树。

因此在一个结点连同其子树都输出完毕后,将curNode置为0,这样跳过循环头部的“循环查找左子树”过程之后再pop栈中元素,从而直接进入判定阶段。

代码:

 /**
* 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) {
TreeNode* curNode = root;
TreeNode* lastNode = NULL;
stack<TreeNode *> nodes;
vector<int> res; while (curNode != NULL || !nodes.empty()) {
while (curNode) {
nodes.push(curNode);
curNode = curNode->left;
} curNode = nodes.top();
if (curNode->right && curNode->right != lastNode) {
curNode = curNode->right;
} else {
res.push_back(curNode->val);
lastNode = curNode;
nodes.pop();
curNode = NULL;
}
} return res;
}
};

【Leetcode】【hard】Binary Tree Postorder Traversal的更多相关文章

  1. 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)

    Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...

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

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

  3. LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)

    145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...

  4. LeetCode: Binary Tree Postorder Traversal 解题报告

    Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...

  5. 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal

    详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal            Given a binary tree, return the po ...

  6. Binary Tree Preorder Traversal and Binary Tree Postorder Traversal

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  7. 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator

    144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

  8. [Leetcode 144]二叉树前序遍历Binary Tree Preorder Traversal

    [题目] Given a binary tree, return the preordertraversal of its nodes' values. Example: Input: [1,null ...

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

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

  10. (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal

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

随机推荐

  1. 【ZooKeeper】单机伪集群搭建(适用于mac)

    1.配置 .zookeeper下载地址:http://apache.mirrors.lucidnetworks.net/zookeeper/ 可以选择需要的版本,我下载的是zookeeper-3.4. ...

  2. 几个用Python实现的简单算法

    一.算法题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 程序分析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去 掉不满足条件的排列. 程序源 ...

  3. Dubbo源码解读

    1.提升SOA的微服务架构设计能力   通过读dubbo源码是一条非常不错的通往SOA架构设计之路,毕竟SOA的服务治理就是dubbo首先提出来的,比起你去看市面上的SOA微服务架构的书籍,学到的架构 ...

  4. Spring MVC 使用kaptcha生成验证码

    Spring MVC 使用kaptcha生成验证码 1.下载kaptcha-2.3.2.jar(或直接通过该文章附件下载) http://code.google.com/p/kaptcha/downl ...

  5. Oracle中的自连接(self join)-当表中的某一个字段与这个表中另外字段的相关时,我们可能用到自连接。

    http://blog.163.com/wkyuyang_001/blog/static/10802122820091751049479/ 当表中的某一个字段与这个表中另外字段的相关时,我们可能用到自 ...

  6. IOS项目之分层MVVM

    在做.Net时,有用到三层架构,使项目分层.ios项目使用AFNetWork把网络层这块也放进了ViewController中,数据解析缓存这些也在里面,这样层次结构可能不够清楚,今天就试着分离了一下 ...

  7. WCF Data Services 5.0 for OData V3

    https://www.microsoft.com/en-us/download/details.aspx?id=29306 VS 2010 下 安装 WCF Data Services 5.0 en ...

  8. 通过POST请求上传文件

    转自:https://blog.csdn.net/zhangge3663/article/details/81218488 理论 简单的HTTP POST 大家通过HTTP向服务器发送POST请求提交 ...

  9. javascript window.opener的用法分析

    window.opener 返回的是创建当前窗口的那个窗口的引用 window.opener 的用法 window.opener 返回的是创建当前窗口的那个窗口的引用,比如点击了a.htm上的一个链接 ...

  10. Asp.Net 之Jquery知识点运用

    1.先把要用的body内的代码写好. <div id="ulBox"> <h3>下面的Ulid为"ulList1"</h3> ...