[LeetCode]题解(python):145-Binary Tree Postorder Traversal
题目来源:
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的更多相关文章
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
- 二叉树前序、中序、后序非递归遍历 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】145. Binary Tree Postorder Traversal (3 solutions)
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
- LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)
翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...
- 145. Binary Tree Postorder Traversal
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...
- 【LeetCode】145. Binary Tree Postorder Traversal 解题报告 (C++&Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- Java for LeetCode 145 Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- leetcode 145. Binary Tree Postorder Traversal ----- java
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
随机推荐
- SQL Server数据库--》top关键字,order by排序,distinct去除重复记录,sql聚合函数,模糊查询,通配符,空值处理。。。。
top关键字:写在select后面 字段的前面 比如你要显示查询的前5条记录,如下所示: select top 5 * from Student 一般情况下,top是和order by连用的 orde ...
- 为什么析构函数要加visual?
一般来说,有使用visual的类,其析构函数都需要加上visual,这是为什么呢? 因为使用多态时,一般都是由基类指针来使用派生类的,在删除这个派生类对象时也一样,由基类指针来调用, 可以想象,如果析 ...
- C++中引用用于结构
正确 void change(test &target) { target.name = "aaa"; } 错误 void change(const test &t ...
- Module 模式 以及 揭示模式。
---恢复内容开始--- Module模式 : 在传统软件工程中为类提供私有和公有封装的方法. 在js中: Module 模式 使用闭包封装 私有状态和组织. 该模式,返回一个公有的API,而其他的一 ...
- MySQL之外键约束
MySQL之外键约束 MySQL有两种常用的引擎类型:MyISAM和InnoDB.目前只有InnoDB引擎类型支持外键约束.InnoDB中外键约束定义的语法如下: [CONSTRAINT [symbo ...
- hdu 2276 Kiki & Little Kiki 2 矩阵快速幂
题目链接 n个灯围成一圈, 1左边是n. 有两种状态, 1是亮, 0是不亮. 如果一个灯, 它左边的灯是亮的, 那么下一时刻这个灯就要改变状态, 1变为0, 0变为1. 给出初始状态和时间t, 问t时 ...
- vb6.0 时间日期
使用year(now)可以得到4位数的年 你还可以用Format来得到, 还有FormatDateTime 下面两种都是一样的结果: FormatDateTime(now,vbLongDate ...
- SD和SDHC和SDXC卡的区别是什么
SD卡,SDHC卡,SDXC卡区别在规格不一样,SD卡最大支持2GB容量,SDHC 最大支持32GB容量,SDXC 最大支持2TB(2048GB)容量,支持SDXC卡的数码设备是兼容支持SD卡与SDH ...
- shell 学习笔记1501-1800
.巧用bash的{}扩展备份目录: cp file.txt{,.bak} .利用at执行一次性命令: echo "ls -l" | at midnight #Execute a c ...
- iOS6和iOS7代码的适配(4)——tableView
iOS7上不少控件的样子有了变化(毕竟要扁平化嘛),不过感觉变化最大的肯定非tableView莫属.因为这个控件的高度可定制性,原先是使用及其广泛的,这样的一个改变自然也影响颇大. 1.accesso ...