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 ...
随机推荐
- 在Win7 环境使用Java API 上传文件到 Hadoop2.x HDFS 问题统计
问题一: org.apache.hadoop.security.AccessControlException: org.apache.hadoop.security .AccessControlExc ...
- [ALM]一步一步搭建MS ALM环境 - 安装TFS + SQL SERVER
描述: 安装SQL SERVER 2012,安装TFS 2013,配置TFS,挽起袖子,准备干活儿 步骤: 1,打开Hyper-V Manager,参考[Hyper-V]使用操作系统模板创建新的虚拟机 ...
- VirtualBox安装Ubuntu教程
1.VirtualBox虚拟机安装,及VirtualBox安装Ubuntu教程VirtualBox版本为VirtualBox-4.3.12-93733-Win.exe,Ubuntu版本为ubuntu- ...
- C语言实现快速排序
我觉得冒泡排序是比较简单的: 所以今天我们实现一个叫做快速排序的: Problem 你想要将(4,3,5,1,2)排序成(1,2,3,4,5) 你决定使用最简单的快速排序: Solution 首先,打 ...
- nginx的反向代理和负载均衡的区别
反向代理,是把一些静态资源存储在服务器上,当用户有请求的时候,就直接返回反向代理服务器上的资源给用户,而如果反向代理服务器上没有的资源,就转发给后面的负载均衡服务器,负载均衡服务器再将请求分发给后端的 ...
- 无线电源传输 Wireless Power Consortium (WPC) Communication
Universally Compatible Wireless Power Using the Qi Protocol Wireless charging of portable electronic ...
- 【由VerySky原创】CX51、CX52 ——数据表
今天通过DEBUG CX52 得出所保存的数据表是ECMCA:
- 字符串匹配的KMP算法——Python实现
#! /usr/bin/python # coding=utf-8 """ 基于这篇文章的python实现 http://blog.sae.sina.com.cn/arc ...
- 友好解决POI导入Excel文件行是不是为空
继 解决POI读取Excel如何判断行是不是为空 后发现了一个问题.这个是一个银行的需求,有20万个客户的资料要导入系统,但有的资料是有问题的(不能正常导入),但也有能正常导入的.现在的问题是怎么知道 ...
- Zabbix监控Windows事件日志
1.zabbix_agentd.win文件修改: LogFile=c:\zabbix\zabbix_agentd.log Server=1.16.2.4 ServerActive=1.16.2.4 H ...