原题链接:https://oj.leetcode.com/problems/binary-tree-postorder-traversal/

题目大意:后序遍历二叉树

解题思路:后序遍历二叉树的步骤:后序遍历二叉树的左子树,后序遍历二叉树的右子树,訪问根结点。

非递归实现时,用一个栈模拟遍历过程。由于訪问完左子树后訪问右子树。栈中元素要起到转向訪问其右子树的作用,可是不能像先序和中序遍历那样出栈就可以,由于根结点时最后訪问的。那么什么时候出栈呢?我们须要一个指针pre来记录前一次訪问的结点。假设pre是根结点的右子树,则说明根结点的右子树訪问完了,此时根结点就能够出栈了。

class Solution{
public:
vector<int> postorderTraversal(TreeNode *root) {
vector<int> res;
stack<TreeNode*> s;
TreeNode* pre=NULL;
while(root||!s.empty())
{
if(root)
{
s.push(root);
root=root->left;
}
else if(s.top()->right!=pre)
{
root=s.top()->right;
pre=NULL;
}
else
{
res.push_back(s.top()->val);
pre=s.top();
s.pop();
}
}
return res;
}
};

时间复杂度:O(N),每一个结点訪问仅一次。

空间复杂度:O(lgN)。即栈的大小树深。

后序遍历是三种遍历中最难得一种,与先序遍历和中序遍历的差别就是:须要一个指针来辅助推断能否够訪问根结点。

Binary Tree Postorder Traversal --leetcode的更多相关文章

  1. Binary Tree Postorder Traversal leetcode java

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

  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. 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)

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

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

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

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

  8. 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

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

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

随机推荐

  1. 解决myeclipse不编译的方法

    请按照下面方法对号入座: MyEclipse不编译解决1. 确保 project->build automatically 已经被选上(最基本). MyEclipse不编译解决2. 如果选上了, ...

  2. rocketmq,zookeeper,redis分别持久化的方式

    1.rocketmq持久化: RocketMQ 的所有消息都是持久化的, 先写入系统 PAGECACHE, 然后刷盘, 可以保证内存与磁盘都有一份数据,访问时,直接从内存读取. RocketMQ 的所 ...

  3. [转载] Spring3.1 Cache注解

    需要感慨一下,spring3.0时丢弃了2.5时的spring-modules-cache.jar,致使无法使用spring来方便的管理cache注解,好在3.1.M1中增加了对cache注解的支持, ...

  4. jquery load 方法回显数据

    使用jQuery对象.load()方法 load() 方法的作用是可以通过 AJAX 请求从服务器加载数据,并把返回的数据直接放置到指定的元素中. 该方法使用起来非常简单,大大简化了ajax开发 语法 ...

  5. Openshift 3.6 安装

    因为有客户需求,所以必须尝试一下,可悲的是手里只有3.7的离线安装文档,加上之前3.11安装因为同事文档写得太好,基本没遇到什么坑,所以就没仔细研究就开始搞了. 结果果然是因为/etc/ansible ...

  6. synchronized 线程同步

    synchronized  通常用来形容一次方法的调用,调用一旦开始,调用者必须等到方法调用返回后,才能继续执行后续的操作. 1.demo package demo1; public class My ...

  7. iOS:Reachability网络监听

    iOS利用Reachability确认网络环境3G/WIFI 开发Web等网络应用程序的时候,需要确认网络环境,连接情况等信息.如果没有处理它们,是不会通过Apple的审查的,一般情况下,可以把网络监 ...

  8. PL/SQL如何远程连接ORACLE

    如何在没有装ORACLE的电脑上用PLSQL远程连接ORACLE 下载instantclient,我的是WIN7,下载的是instantclient-basiclite-nt-12.1.0.1.0.z ...

  9. 将思维转向rss

    本屌丝因为穷住在了离市区比较远的农民房,平时上下班单程地铁时间接近一小时.在这漫长的一小时里,总得干点什么来蹉跎这段时光,看手机是最容易实现的事情.最地铁信号不好,手机也没什么好看的. 经过高人指点说 ...

  10. Android RxJava/RxAndroid结合Retrofit使用

    概述 RxJava是一个在 Java VM 上使用可观測的序列来组成异步的.基于事件的程序的库.更重要的是:使用RxJava在代码逻辑上会非常简洁明了,尤其是在复杂的逻辑上.告别迷之缩进. RxAnd ...