原题链接: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. 【mysql】新方法修改数据库密码以及解决--ERROR 1045 (28000)的问题

    之前 有写过一篇修改mysql数据库的密码的一篇随笔, 地址是:http://www.cnblogs.com/sxdcgaq8080/p/5667124.html 但是此次采用原本的老方法,出现了问题 ...

  2. es6数组去重复

    var arr=[1,1,2,3,5,7,7,7] arr=Array.from(new Set(arr))

  3. String格式化参数整理

    Java String格式话参数整理如下: conversion:转换格式,可选的格式有: d 整数型(十进制) c Unicode字符 b Boolean值 s String f 浮点数(十进制) ...

  4. 模糊搜索:concat各种函数详解、like操作符、通配符

    if(StringUtils.isNotBlank(queryBean.getConditions())){ hqlBuilder.addWhereClause(" concat(this. ...

  5. 转: git的图文使用教程(巨详细)

    转自: http://blog.jobbole.com/78960/ Git使用教程 一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 二:SVN与Git的最主要的区别? SVN是集 ...

  6. JMS与Spring之二(用message listener container异步收发消息)

    转自:http://blog.csdn.net/moonsheep_liu/article/details/6684948

  7. poj 2253 (dis最短路径)

    Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24979   Accepted: 8114 Descript ...

  8. python中,== 与 is 之间区别

    在python中,== 与 is 之间既有区别,又有联系,本文将通过实际代码的演示,力争能够帮助读到这篇文章的朋友以最短的时间理清二者的关系,并深刻理解它们在内存中的实现机制. 扯淡的话不多说,下面马 ...

  9. JBoss 系列十八:使用JGroups构建块RpcDispatcher构建群组通信应用

    内容概要 本部分说明JGroups构建块接口RpcDispatcher,具体提供一个简单示例来说明如何使用JGroups构建块RpcDispatcher构建群组通信应用. 示例描述 类似Message ...

  10. javascript闭包传参就这么简单

    var query = (function (a) { return a; })('fx'); alert(query);