原题链接: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. 修改activityMQ的登录账与密码

    登录下管理员页面,ip根据实际的来 URL : http://127.0.0.1:8161/admin/ 默认账户密码都是admin 账户密码修改在conf文件夹下的jetty-realm.prope ...

  2. Hive技术拾遗

    1. SELECT语句可以使用正则表达式做列选择,下面的语句查询除了ds和h 之外的所有列:SELECT `(ds|hr)?+.+` FROM sales 2. LEFT SEMI JOIN的限制是, ...

  3. WebSocket 是什么原理?为什么可以实现持久连接?(转载)

    本文转载自知乎,来源如下: 作者:Ovear链接:https://www.zhihu.com/question/20215561/answer/40316953来源:知乎著作权归作者所有.商业转载请联 ...

  4. http://my.oschina.net/China2012/blog/178655

    http://my.oschina.net/China2012/blog/178655 http://git.oschina.net/huangyong/smart-framework

  5. 第九章:Elasticsearch集群优化及相关节点配置说明

    Linux系统调优: Linux调整打开文件数(重新启动生效) 在/etc/security/limits.conf在文件中增加: * soft nofile 8192 * hard nofile 2 ...

  6. 假设这篇文章也能訪问过万,我的那些朋友就是SB

    能和我这个疯子交朋友的一定是神经病

  7. dubbo-monitor安装监控中心,管理控制台安装

    一.安装监控中心 1.创建安装目录 2.解压 上传文件解压文件 解压 3.修改配置文件 4.启动 如果一直出现点.只需要加大内存即可,内存至少大于1024,然后reboot重启 5.测试 二.安装管理 ...

  8. JMeter 十六:加密处理

    假设采用MD5进行加密 JMeter 内置的没有MD5加密方法.网上有说采用__MD5函数的,但是我在 Jmeter 2.13 以及 Jmeter 3.2 版本上都没有找到这个函数,官方文档也没有看到 ...

  9. Unity3D入门基础之游戏对象 (GameObject) 和组件 (Component) 的关系

    原文出处:http://edu.china.unity3d.com/learning_document/getData?file=/Manual/TheGameObject-ComponentRela ...

  10. Bitmap和Drawable的互相转换

    刚好之前的项目实用到.怕遗忘了.就先记录下来.然后会用到的时候直接来这copy使用就好了. 1.Bitmap ---->Drawable: public static Drawable bitm ...