Binary Tree Postorder Traversal--leetcode难题讲解系列
https://leetcode.com/problems/binary-tree-postorder-traversal/
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].
Note: Recursive solution is trivial, could you do it iteratively?
后续遍历二叉树,要求不使用递归。
很明显需要使用栈来进行迭代。后续遍历是依序访问左子树,右子树,根节点。我们的入口是根节点,那么我们的栈应该先保存根,然后右子树,再保存左子树。
分开来看,根的左孩子有可能是根而不是叶子结点,我们需要一路向下保存根节点,直到遇到叶子结点,才能保证根最先保存。
怎么样保证右子树比左子树先保存呢?事实上我们访问的顺序是先左后右。现在我们只需要保证右子树比根节点优先输出(后入栈),而左子树已全部出栈就可以。怎么判断什么时候入栈(第一次遇到根(左孩子),第一次遇到右孩子),什么时候出栈(第二次遇到右孩子,第二次遇到根(左孩子))?我们需要有一个pre变量来记录之前遇到的最后一个节点。如果pre和当前节点的右孩子值相等,那么我们是第二次遇到根,此时右孩子早已访问,直接根出栈。否则,我们应访访问当前节点的右孩子(入栈)。
// C++ CODE:
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
stack<TreeNode*> s;
vector<int> result;
while(root){
s.push(root);
root = root->left;
}
while(!s.empty()){
TreeNode* tmp = s.top();
if(tmp->right){
if(result.empty()||tmp->right->val !=result[result.size()-]){
TreeNode* tmproot = tmp->right;
while(tmproot){
s.push(tmproot);
tmproot = tmproot->left;
}
}else{
result.push_back(tmp->val);
s.pop();
}
}else{
result.push_back(tmp->val);
s.pop();
}
}
return result;
}
};
# PYTHON CODE:
class Solution:
# @param root, a tree node
# @return a list of integers
def postorderTraversal(self, root):
stack, cur, pre, res = [], root, None, []
while cur or stack:
if cur:
stack.append(cur)
cur = cur.left
else:
if stack[-1].right in (None, pre):
res.append(stack[-1].val)
pre = stack.pop()
else:
cur = stack[-1].right
return res
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 ...
- Binary Tree Postorder Traversal --leetcode
原题链接:https://oj.leetcode.com/problems/binary-tree-postorder-traversal/ 题目大意:后序遍历二叉树 解题思路:后序遍历二叉树的步骤: ...
- 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 ...
随机推荐
- hibernate date类型插入数据库时精度只到日期没有时间
由hibernate 的逆向工具从数据库表生成的*.hbm.xml ,对于数据库的date类型生成如下: <property name = "crttime" ...
- Jenkins pipeline 入门到精通系列文章
Jenkins2 入门到精通系列文章. Jenkins2 下载与启动jenkins2 插件安装jenkins2 hellopipelinejenkins2 pipeline介绍jenkins2 jav ...
- hasOwnProperty()&&isPrototypeOf()
1.hasOwnProperty() hasOwnProperty()函数用于指示一个对象自身(不包括原型链)是否具有指定名称的属性.如果有,返回true,否则返回false. 该方法属于Object ...
- [python爬虫] Selenium定向爬取海量精美图片及搜索引擎杂谈
我自认为这是自己写过博客中一篇比较优秀的文章,同时也是在深夜凌晨2点满怀着激情和愉悦之心完成的.首先通过这篇文章,你能学到以下几点: 1.可以了解Python简单爬取图片的一些思路和方法 ...
- Java程序员的日常—— 《编程思想》关于类的使用常识
Java虽然利用JVM,让程序员可以放心大胆的使用,可是仍然会出现内存泄露等问题.世上没有绝对的银弹,因此也不能完全把所有的任务都交给JVM,了解Java中的初始化与垃圾回收还是必不可少的知识. 关于 ...
- jQuery实现左移右移
<html> <head> <meta charset="utf-8"> <title>完成左移右移</title> & ...
- 记忆化搜索hdu1078 dfs
http://acm.hdu.edu.cn/showproblem.php?pid=1078 题意:给出n*n的格子,每个各自里面有些食物,问一只老鼠每次走最多k步所能吃到的最多的食物 #includ ...
- iOS5可能会删除本地文件储存 - Caches 也不安全
转自:http://blog.163.com/ray_jun/blog/static/1670536422011101225132544/ 出处:http://superman474.blog.163 ...
- Objective-C 语法之 static 关键字
转:http://www.apkbus.com/android-593-1.html 学习过Java 或者 C 语言的朋友应该很清楚static关键字吧?在某个类中声明一个static 静态变量, 其 ...
- Find Minimum in Rotated Sorted Array leetcode java
题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...