[leetcode]Binary Tree Level Order Traversal II @ Python
原题地址: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.
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}"
.
# 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的更多相关文章
- [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 ...
- [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 ...
- 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 ...
- LeetCode - Binary Tree Level Order Traversal II
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- 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 ...
- LeetCode Binary Tree Level Order Traversal II (二叉树颠倒层序)
题意:从左到右统计将同一层的值放在同一个容器vector中,要求上下颠倒,左右不颠倒. 思路:广搜逐层添加进来,最后再反转. /** * Definition for a binary tree no ...
- LeetCode:Binary Tree Level Order Traversal I II
LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...
- 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 ...
- 【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 ...
随机推荐
- php利用root权限执行shell脚本 (转)
转一篇博客,之前搞这个东西搞了好久,结果今天晚上看到了一篇救命博客,瞬间开心了...转载转载 利用sudo来赋予Apache的用户root的执行权限,下面记录一下: 利用PHP利用root权限执行sh ...
- gp数据库运维
最近需要将一份db2导出的历史数据入库gp集群,然后把每天的增量数据导出成txt文件和对应的log日志,再ftp传输给另外一台机器.其中陆续碰到一些坑,在此记录 历史文件数据清洗 列分隔符的选择 碰到 ...
- 那些年我们爬过的山 - mybatis批量导入
[原创作品,转载请注明出处] 写这篇文章之前想着给这篇博客起一个文艺一点的标题,思来想去,想到了那些年我们爬过的山,或者我们一起趟过的河?代码不规范,同事两行泪,这是多么痛的领悟啊! 背景 本组一名实 ...
- zoj 3983 Crusaders Quest 思维+枚举
题目链接 这道题意思是: 给你一个长度为9的字符串,且只有3个a.3个g.3个o 问,你可以选择删除一段连续的串或者单个的字符也可以不删,最多会出现几个三子相连的子串 比如:agoagoago只有将两 ...
- [leetcode greedy]45. Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- hdu 4025 Equation of XOR 状态压缩
思路: 设: 方程为 1*x1 ^ 1*x2 ^ 0*x3 = 0; 0*x1 ^ 1*x2 ^ 1*x3 = 0; 1*x1 ^ 0*x2 ^ 0*x3 = 0 把每一列压缩成一个64位整数,因为x ...
- php模块组成
php总共有三个模块:内核.ZEND引擎.扩展. 内核是用来处理请求.文件流.错误处理等操作的: ZEND引擎是将源文件转换成机器语言,然后在虚拟机上运行: 扩展层是一组函数.类库和流,php使用它们 ...
- 小程序在wxml页面中取整
小程序无法像html中,在页面中直接parseInt() index.wxml {{price | Int}} 小程序还有另一种处理方法 wxs 是一种类似于js脚本的东西 filters.wxs v ...
- 使用邮件和RSS两种方式,订阅博客更新通知
分类: 系统运维 点击订阅按钮,可以订阅本博客的更新 输入您的邮件地址,可以订阅本博客的更新通知,及时了解最新内容 使用RSS,订阅-马二进三名人传记-博客 也许大家是第一次听到RSS这个概念,那什么 ...
- Java学习笔记_22_Set接口的实现类
22.Set接口的实现类: Set接口存放的元素是无序的且不包括反复元素. 1>实现类HashSet: HashSet类依据元素的哈希码进行存放,取出时也能够依据哈希码高速找到.HashSet不 ...