Binary Tree Postorder Traversal --leetcode
原题链接: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的更多相关文章
- Binary Tree Postorder Traversal leetcode java
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bin ...
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
- LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)
145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...
- LeetCode: Binary Tree Postorder Traversal 解题报告
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal
详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal Given a binary tree, return the po ...
- 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 ...
- 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
随机推荐
- 2011年排名前七位的Linux操作系统。
下面列出了2011年排名前七位的Linux操作系统. Ubuntu Ubuntu 是一个由全球化的专业开发团队建造的操作系统.它包含了所有您需要的应用程序:浏览器.Office 套件.多媒体程序.即时 ...
- mac -- 安装OpenCV
brew install opencv #这个装的是3.4 brew unlink opencv # 取消关联 brew install opencv@2 # 安装2.X的版本
- windows2012 IIS部署GeoTrust证书踩过的坑。
系统:windows2012 环境:IIS8 在阿里云上买了GeoTrust证书, 按照说明下载证书到服务器, 导入证书, 给IIS站点部署https. 阿里云部署帮助文档:https://help ...
- JSON相关
- juqery.fn.extend和jquery.extend
jquery.fn == jquery.prototype //true jquery.extend( obj1,obj2 ) 用一个或多个对象来拓展一个对象,返回拓展之后的对象 var aaa = ...
- [Git] 给git命令起别名
转载自:http://blog.csdn.net/qinjienj/article/details/7479886 程序员都是"懒惰"的,哪怕是执行一个命令的时候少敲了一个字母也感 ...
- iOS:quartz2D绘图(给图形绘制阴影)
quartz2D既可以绘制原始图形,也可以给原始图形绘制阴影. 绘制阴影时,需要的一些参数:上下文.阴影偏移量.阴影模糊系数 注意:在drawRect:方法中同时调用绘制同一个图形时,在对绘制的图形做 ...
- OTL调用存储过程/函数及注意事项
OTL 是 Oracle, Odbc and DB2-CLI Template Library 的缩写,是一个 C++ 编译中操控关系数据库的模板库,它目前几乎支持所有的当前各种主流数据库. OTL ...
- pyPdf - 用Python方便的处理PDF文档
pyPdf库 ( http://pybrary.net/pyPdf/ ) ,操作起来相当直接易懂,把代码贴在这儿,做个记录. 1 from pyPdf import PdfFileWriter, P ...
- 项目部署问题:xftp无法连接服务器、Nginx403 Forbidden解决、nginx反向代理解决前端跨域问题
一.xftp无法连接服务器 在xftp中配置正确的ip,用户名和密码后,居然无法连接 解决方案:将协议里面的FTP换成SFTP,注意换成SFTP后端口就默认换成22,要还是原来的21就还是连不上的哈 ...