题目来源:

  https://leetcode.com/problems/binary-tree-postorder-traversal/


题意分析:

  后序遍历一棵树,递归的方法很简单,尝试用非递归的方法。


题目思路:

  后序遍历的顺序是,先左子树,再右子树,最后才是根节点。递归的思想很简单,那么非递归的方法也是利用栈来实现,后进先出,不过这里先进的应该是左子树,那么得到的结果是根节点,右子树接着左子树。最后将结果翻转就可以了。代码给的是非递归的方法。


代码(python):

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def postorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
ans = []
if root == None:
return ans
stack = [root]
while len(stack) != 0:
p = stack.pop()
ans.append(p.val)
if p.left:
stack.append(p.left)
if p.right:
stack.append(p.right)
ans.reverse()
return ans

[LeetCode]题解(python):145-Binary Tree Postorder Traversal的更多相关文章

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

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

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

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

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

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

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

  5. (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...

  6. LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)

    翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...

  7. 145. Binary Tree Postorder Traversal

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

  8. 【LeetCode】145. Binary Tree Postorder Traversal 解题报告 (C++&Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

  9. Java for LeetCode 145 Binary Tree Postorder Traversal

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

  10. leetcode 145. Binary Tree Postorder Traversal ----- java

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

随机推荐

  1. spring框架详解

    把之前分享的spring框架整理一份放在这里. 整体架构: Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架 框架图(选自:http://docs.spring.io/spr ...

  2. Linux_常用命令

    文件搜索 -find -locate -grep 字符串搜索 -grep 过滤 -grep/find/xargs/ 编辑 -sed 待续....

  3. mysql性能监控工具Innotop

    mysql监控管理工具--innotop 1.innotop安装参考官网:http://innotop.googlecode.com/svn/html/installing.html # wget h ...

  4. Linux简介(好!)

    Linux操作系统介绍 来源:233网校论文中心[ 2009-12-02 14:23:00 ]阅读:1作者:王长青编辑:studa20 [摘 要]文章从Unix.Minix系统的产生引出了Linux操 ...

  5. 管理node_modules

    http://stackoverflow.com/questions/15225865/centralise-node-modules-in-project-with-subproject

  6. <转>eclipse如何修改dynamic web module version .

         --------------------------------------------------------------------------------------------- 原 ...

  7. Thinkphp的时间判断

    在做项目的过程中,非常频繁地遇到时间这个问题,像时间的比较,特定时间执行某一操作,但是现在只解决了一部分问题,先说明一下时间的判断问题. 很简单,时间,不断使date(),now(),都是字符串类型的 ...

  8. this的用法this.name=name 这个什么意思

    public Employee(string name, string alias){ // Use this to qualify the fields, name and alias: this. ...

  9. hdu 4676 Sum Of Gcd 莫队+数论

    题目链接 给n个数, m个询问, 每个询问给出[l, r], 问你对于任意i, j.gcd(a[i], a[j]) L <= i < j <= R的和. 假设两个数的公约数有b1, ...

  10. [LeetCode]题解(python):153-Find Minimum in Rotated Sorted Array

    题目来源: https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ 题意分析: 在一个不重复的翻转的数组里面找到最小那个 ...