原题地址: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. 洛谷P4645 [COCI2006-2007 Contest#7] BICIKLI [Tarjan,拓扑排序]

    题目传送门 BICIKLI 题意翻译 给定一个有向图,n个点,m条边.请问,1号点到2号点有多少条路径?如果有无限多条,输出inf,如果有限,输出答案模10^9的余数. 两点之间可能有重边,需要看成是 ...

  2. CTF实验吧让我进去writeup

    初探题目 两个表单,我们用burp抓包试试 这时候我们发现Cookie值里有个很奇怪的值是source,这个单词有起源的意思,我们就可以猜测这个是判断权限的依据,让我们来修改其值为1,发送得到如下显示 ...

  3. 【原创】MHA对MySQL master Too many connections错误的处理机制

    有台服务器故障期间的现象: 1. 可以正常ping通 2. telnet服务端口报Too many connections错误 3. ssh连接不上 查看MHA的管理日志,在强制关机前的health ...

  4. Bzoj4548 小奇的糖果(链表+树状数组)

    题面 Bzoj 题解 很显然,我们只需要考虑单独取线段上方的情况,对于下方的把坐标取反再做一遍即可(因为我们只关心最终的答案) 建立树状数组维护一个横坐标区间内有多少个点,维护双向链表实现查询一个点左 ...

  5. 【转】让你10分钟搞定Mac--最简单快速的虚拟安装

    文章出处:让你10分钟搞定Mac--最简单快速的虚拟安装http://bbs.itheima.com/thread-106643-1-1.html (出处: 黑马程序员训练营论坛) 首先说明一下. 第 ...

  6. [xsy3343]程序锁

    题意:有两个序列,序列中数字$\in\{-1,0,1\}$ 有两个指针,初始时分别指向两个序列的开始位置,有一个初始为$0$的数$a$,重复以下过程直到两个指针都指向序列末尾后 如果一个指针指向末尾后 ...

  7. Git 工具的使用,windows平台安装

    先谈谈版本控制的一些事 如果你严肃对待编程,就必定会使用"版本控制系统"(Version Control System). 随着信息科技的发展,软件开发已不是小手工作坊,软件的规模 ...

  8. Git 初学者使用指南及Git 资源整理

    Git 资源整理 Git is a free and open source distributed version control system designed to handle everyth ...

  9. UVALive 5058 Counting BST 数学

    B - Counting BST Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit S ...

  10. 机器学习(1):Logistic回归原理及其实现

    Logistic回归是机器学习中非常经典的一个方法,主要用于解决二分类问题,它是多分类问题softmax的基础,而softmax在深度学习中的网络后端做为常用的分类器,接下来我们将从原理和实现来阐述该 ...