题目来源


https://leetcode.com/problems/binary-tree-level-order-traversal-ii/

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).


题意分析


Input: binary tree

Output: list

Conditions:输出每层的集合,注意是反向输出,即最后一层的先输出


题目思路


同I,将res反置即可


AC代码(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 preorder(self, root, level, res):
if root:
if len(res) < level + 1: res.append([])
res[level].append(root.val)
self.preorder(root.left, level + 1, res)
self.preorder(root.right, level + 1, res)
def levelOrderBottom(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
res = []
self.preorder(root, 0, res)
res.reverse()
print res
return res

[LeetCode]题解(python):107 Binary Tree Level Order Traversal II的更多相关文章

  1. Leetcode PHP题解--D125 107. Binary Tree Level Order Traversal II

    val = $value; } * } */ class Solution { private $vals = []; /** * @param TreeNode $root * @return In ...

  2. 102/107. Binary Tree Level Order Traversal/II

    原文题目: 102. Binary Tree Level Order Traversal 107. Binary Tree Level Order Traversal II 读题: 102. 层序遍历 ...

  3. 【LeetCode】107. Binary Tree Level Order Traversal II (2 solutions)

    Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal ...

  4. [LeetCode] 107. Binary Tree Level Order Traversal II 二叉树层序遍历 II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  5. 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  6. Java for LeetCode 107 Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  7. LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)

    翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...

  8. (二叉树 BFS) leetcode 107. Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  9. 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...

随机推荐

  1. BZOJ1397 : Ural 1486 Equal squares

    二分答案mid,然后检验是否存在两个相同的mid*mid的正方形 检验方法: 首先对于每个位置,求出它开始长度为mid的横行的hash值 然后对于hash值再求一次竖列的hash值 将第二次求出的ha ...

  2. Java中正则表达式、模式匹配与信息抽取

    引言 记得几年前在做网页爬虫后的信息抽取时,针对网页源码中隐藏的要提取的信息,比如评论.用户信息等属性信息,直接利用HtmlParser得到.如此做倒是简单,不过利用的是网页的规范的tag标记.其实j ...

  3. CentOS 下安装无线哥的老爷机DELL的无线驱动

    使用命令检测网卡 lspci | grep Network   为“0c:00.0 Network controller: Broadcom Corporation BCM4312 802.11b/g ...

  4. DOS命令下输入:java Hello 出现以下几种结果可能的原因:

    DOS命令下输入:java Hello 出现以下结果:Bad command or the file name 没有这个命令或文件名 原因可能是没有成功安装jdk或者没有配置好jdk 的环境变量,或者 ...

  5. 数组机、局域网ip查找

    cmd ipconfig 以太网适配器 VMware Network Adapter VMnet8: IPv4 地址 . . . . . . . . . . . . : 192.168.233.1

  6. SQLLite 可以通过SQL语言来访问的文件型SQL数据库

    Web Storage分为两类: - sessionStorage:数据保存在session 对象中(临时) - localStorage:数据保存在本地硬件设备中(永久) sessionStorag ...

  7. CentOS源列表

    vi /etc/yum.repos.d/CentOS-Base.repo CentOS 5: # CentOS-Base.repo # # The mirror system uses the con ...

  8. ORA-12518: TNS: 监听程序无法分发客户机连接

    在团队成员增多时,经常出现“无法分发客户端连接”等问题.在网上搜索一番后,最终解决了该问题,现将解决方案总结如下,以供参考和以后备用. 原因:团队成员增多,原有数据库设置不够用,导致连接plsql和启 ...

  9. Bungie Interview with Halo3 Developer

    http://www.realtimerendering.com/blog/tag/bungie/ Digital Foundry interview with Halo: Reach develop ...

  10. Apache Spark源码走读之20 -- ShuffleMapTask计算结果的保存与读取

    欢迎转载,转载请注明出处,徽沪一郎. 概要 ShuffleMapTask的计算结果保存在哪,随后Stage中的task又是如何知道从哪里去读取的呢,这个过程一直让我困惑不已. 用比较通俗一点的说法来解 ...