[LeetCode&Python] Problem 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 to right, level by level from leaf to root).
For example:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its bottom-up level order traversal as:
[
[15,7],
[9,20],
[3]
]
# 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 levelOrderBottom(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
if not root:
return []
ans=[]
levelans=[]
que=[root] while que:
n=len(que)
i=0
while i<n:
node=que.pop(0)
levelans.append(node.val)
if node.left:
que.append(node.left)
if node.right:
que.append(node.right)
i+=1
ans.append(levelans)
levelans=[]
return ans[::-1]
[LeetCode&Python] Problem 107. Binary Tree Level Order Traversal II的更多相关文章
- 【leetcode❤python】107. Binary Tree Level Order Traversal II
#-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):# def __init ...
- 102/107. Binary Tree Level Order Traversal/II
原文题目: 102. Binary Tree Level Order Traversal 107. Binary Tree Level Order Traversal II 读题: 102. 层序遍历 ...
- 【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 ...
- [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 ...
- 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- 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 ...
- LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)
翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...
- (二叉树 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 ...
- [LeetCode]题解(python):107 Binary Tree Level Order Traversal II
题目来源 https://leetcode.com/problems/binary-tree-level-order-traversal-ii/ Given a binary tree, return ...
随机推荐
- 转:Linux环境变量设置方法总结 PATH、LD_LIBRARY_PATH
转: https://www.linuxidc.com/Linux/2017-03/142338.htm 文章写比较全 转载记录 Linux环境变量设置方法总结 PATH.LD_LIBRARY_P ...
- js地址多选实现,居住地,户口,职业,行业多选2
需求是根据点击一个按钮 实现动态添加数据,所以每个数据都有一个地址多选, 以下是效果实现及部分关键代码,相关js相关文件在 http://www.cnblogs.com/zhan1995/p/848 ...
- win7下使用U盘安装双系统(Ubuntu-17)
1.首先下载Ubuntu镜像文件,下载地址:http://mirrors.neusoft.edu.cn/ 2.下载 U盘操作系统安装工具- Universal USB Installer ,下载地址: ...
- 逆袭之旅DAY09.东软实训.接口
2018年7月5日 package day0705.teacher.test1usb; /** * 测试类 * @author Administrator * */ public class UsbI ...
- DDR3和eMMC区别
DDR3内存条和eMMC存储器区别: 1. 存储性质不同:2. 存储容量不同:3. 运行速度不同:4. 用途不同. 具体区别如下: 1.存储性质不同:eMMC是非易失性存储器,不论在通电或断电状态下, ...
- cocos2d方块方块
cGridSize=32 cSceneWidth=8+2 cSceneHeight=18 fuction Grid2Pos(x,y) local visibleSize=cc.Director:get ...
- DBProxy 项目全解
转载自:https://github.com/Meituan-Dianping/DBProxy/blob/master/doc/USER_GUIDE.md#2 1 总体信息 1.1 关于 ...
- day24 模块03_re
休养生息 --模块03 1.正则表达式 2.在python中使用正则.re 一,正则表达式 (匹配字符串,主要是给字符串使用的) 1)元字符 . 除换行符之外 \w 数字,字母,下划线组成 \W ...
- sqlalchemy 模型中添加根据身份证号计算性别和年龄
class Users(Base, BaseMixin): username = Column(String(24)) id_Num = Column(String(18) ) # 身份证号码 @pr ...
- Scrapy结构
http://scrapy-chs.readthedocs.io/zh_CN/1.0/intro/overview.html scrapy 使用Twisted 这个异步网络库来处理网络通信,使用pyt ...