原题地址:http://oj.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).

For example:
Given binary tree {3,9,20,#,#,15,7},

    3
/ \
9 20
/ \
15 7

return its bottom-up level order traversal as:

[
[15,7]
[9,20],
[3],
]

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

OJ's Binary Tree Serialization:

The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.

Here's an example:

   1
/ \
2 3
/
4
\
5

The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".

解题思路:由于编程语言为python,所以其实在Binary Tree Level Order Traversal这道题(http://www.cnblogs.com/zuoyuan/p/3722004.html)的基础上加一句res.reverse()就可以了。
代码:
# Definition for a  binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
# @param root, a tree node
# @return a list of lists of integers
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):
res=[]
self.preorder(root, 0, res)
res.reverse()
return res

[leetcode]Binary Tree Level Order Traversal II @ Python的更多相关文章

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

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

  2. [Leetcode] Binary tree level order traversal ii二叉树层次遍历

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

  3. LeetCode——Binary Tree Level Order Traversal II

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

  4. LeetCode - Binary Tree Level Order Traversal II

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

  5. LeetCode "Binary Tree Level Order Traversal II" using DFS

    BFS solution is intuitive - here I will show a DFS based solution: /** * Definition for a binary tre ...

  6. LeetCode Binary Tree Level Order Traversal II (二叉树颠倒层序)

    题意:从左到右统计将同一层的值放在同一个容器vector中,要求上下颠倒,左右不颠倒. 思路:广搜逐层添加进来,最后再反转. /** * Definition for a binary tree no ...

  7. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

  8. LeetCode之“树”:Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal 题目链接 题目要求: Given a binary tree, return the level order traversal o ...

  9. 【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 ...

随机推荐

  1. TradingView 自定义指标

    TradingView 支持自定义指标,不过是把你要定义的指标写成一个 JS 源文件(customIndex.js),放在图表库 static 文件夹下.自定义指标 JS 源代码模板如下: __cus ...

  2. 2017-2018-1 20179202《Linux内核原理与分析》第九周作业

    进程的切换和系统的一般执行过程 1.知识总结 (1)进程调度的时机: 中断处理过程直接调用schedule(),或者返回用户态时根据need_resched标记调用schedule(). 内核线程是一 ...

  3. 预备作业02 : 体会做中学(Learning By Doing)

    1.你有什么技能比大多人(超过班级90%以上)更好? 如果说不算上玩玻璃球在诸如此类不登大雅之堂的技能,我想我是没有什么比大多数人更好的.我的兴趣还算广泛,但很多东西也只是学到了皮毛而已. 在我上初中 ...

  4. MySQL 类型转换

    1.问题描述 在项目中遇到要将Int类型转为Char类型,然后利用转化后的Char类型进行模糊查询. 例:合同编号在数据库中为int类型 8066 用利用 806 模糊查询出合同编号为8066数据记录 ...

  5. Mac 上自带TFTP Server 软件的使用

    搬瓦工搭建SS教程 1.TFTP协议 简单文件传输协议Trivial File Transfer Protocol (TFTP)是一个基于UDP协议的简单的.低开销的文件传输协议,允许客户端get或者 ...

  6. hdu1312 Red and Black

    I - Red and Black Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  7. Luogu 4492 [HAOI2018]苹果树 组合数

    https://www.luogu.org/problemnew/show/P4492 找每个编号的点的父边的贡献,组合数和阶乘就能算了. 我考场上怎么就是没想到呢. 调了好久好久好久好久调不出来,样 ...

  8. 【2017多校训练08 1002】【HDOJ 6134】Battlestation Operational

    典型的数列反演题. 运用莫比乌斯反演的一个结论 $[n = 1] = \sum_{d | n} \mu(d)$,将表达式做如下转化: $$ ans = \sum_{i=1}^n \sum_{j=1}^ ...

  9. 洛谷P2657 Loj10165 SCOI2009 windy数

    题目描述 windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道, 在A和B之间,包括A和B,总共有多少个windy数? 输入输出格式 输 ...

  10. UVALive 5968

    假如出现SS 那么表示Spring,如果出现SX的话,就表示WINTER,末尾出现S不管 #include <map> #include <set> #include < ...