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难题讲解系列的更多相关文章

  1. Binary Tree Postorder Traversal leetcode java

    题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bin ...

  2. Binary Tree Postorder Traversal --leetcode

    原题链接:https://oj.leetcode.com/problems/binary-tree-postorder-traversal/ 题目大意:后序遍历二叉树 解题思路:后序遍历二叉树的步骤: ...

  3. C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)

    145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...

  4. LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)

    145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...

  5. LeetCode: Binary Tree Postorder Traversal 解题报告

    Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...

  6. 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)

    Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...

  7. 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal

    详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal            Given a binary tree, return the po ...

  8. 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 ...

  9. 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

  10. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

随机推荐

  1. load css use javascript

    var $ = document; // shortcut var cssId = 'myCss'; // you could encode the css path itself to genera ...

  2. [ACM_模拟] ACM - Draw Something Cheat [n个长12的大写字母串,找出交集,按字母序输出]

    Description Have you played Draw Something? It's currently one of the hottest social drawing games o ...

  3. [51单片机] TFT2.4彩屏3 [自制动画效果-滑块+吊钩]

    >_<:引脚和前面几个连接一样,这里做了一个实用的动画效果,模拟起重机的2维视图. #ifndef __ILI9325_H__ #define __ILI9325_H__ void ILI ...

  4. js继承精益求精之寄生式组合继承

    一.混合/组合继承的不足 上一篇JS继承终于混合继承,认真思考一下,发现其还是有不足之处的: 空间上的冗余:在使用原型链的方法继承父类的原型属性(Animal.prototype)的同时,也在子类的原 ...

  5. Gershgorin圆盘定理

    众所周知,对一个$n$阶方阵求取特征值需要解一个一元$n$次方程,当$n$很大时,这是很难实现的.但是,在有些涉及矩阵的实际问题中,我们并不需要知道矩阵特征值的准确值,而只需要知道其大概范围就行了,例 ...

  6. Atitit..组件化事件化的编程模型--(2)---------Web datagridview 服务器端控件的实现原理and总结

    Atitit..组件化事件化的编程模型--(2)---------Web datagridview 服务器端控件的实现原理and总结 1. 服务端table控件的几个流程周期 1 1.1. 确认要显示 ...

  7. paip.提升效率--数据绑定到table原理和流程Angular js jquery实现

    paip.提升效率--数据绑定到table原理和流程Angular js  jquery实现 html #--keyword 1 #---原理和流程 1 #----jq实现的代码 1 #-----An ...

  8. java集合——题4,6

    4.(List)写一个函数reverseList,该函数能够接受一个List,然后把该List 倒序排列. 例如: List list = new ArrayList(); list.add(“Hel ...

  9. Head First Design Patterns

    From Head First Design Patterns. Design Principle: Idnetify the aspects of your application that var ...

  10. SQL Server 内存中OLTP内部机制概述(四)

    ----------------------------我是分割线------------------------------- 本文翻译自微软白皮书<SQL Server In-Memory ...