题目来源:

  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. SQLite语法

    一.建立数据库 sqlite3.exe test.db 二.双击sqlite-3_6_16目录下的程序sqlite3.exe,即可运行 三.退出 .exit 或者 .quit 四.SQLite支持如下 ...

  2. 图片上传webuploader

    /** * 基于jquery的图片上传控件 */!function ($) { "use strict"; //定义上传事件 var upImgEvent = { fileQueu ...

  3. char* 和 wchar_t* 如何互相转换

    char* 和 wchar_t* 如何互相转换 C函数可以用 wcstombs - 将宽字符转换成多字符 WCHAR ->  CHAR      mbstowcs - 把多字符把转换成宽字符 C ...

  4. C++_内部类

    C++ 内部类和外部类之间的相互调用

  5. iis7.0/8.0rewrite规则

    首先在网站 web.config 配置文件的 configuration 节点中加入如下代码: <system.webServer> <rewrite> <rules&g ...

  6. Linux下多任务间通信和同步-消息队列

    Linux下多任务间通信和同步-消息队列 嵌入式开发交流群280352802,欢迎加入! 简介 消息队列简称为队列.消息队列就是一些消息的列表.用户可以在消息队列中添加消息和读取消息等.从这点上看,消 ...

  7. 移动端使用rem方法

    (function (doc, win) { var docEl = doc.documentElement, resizeEvt = 'orientationchange' in window ? ...

  8. 重置出错?微软Win10平板Surface Pro 4重装系统教程详解

    重置出错?微软Win10平板Surface Pro 4重装系统教程详解 2015-12-11 15:27:30来源:IT之家作者:凌空责编:凌空 评论:65 Surface Pro 4系统重置出错该怎 ...

  9. WIX 学习笔记- 1 简介

    一个项目 Code Complete 后,程序员们欢欣鼓舞,以为事情到此结束,可以 Happy 了.其实 Code Complete 五十之于百里.一个没有运行在设备上,为人们创造价值的项目是注定失败 ...

  10. 十几个remote control software

    5 alternatives to LogMeIn Free for remote PC access VNC VNC, or Virtual Network Computing, isn’t its ...