Problem Link:

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

Use BFS from the tree root to traverse the tree level by level. The python code is as follows.

# 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 levelOrderBottom(self, root):
"""
BFS from root, and record the values level by level
"""
# List of levels
res = []
# Special case: empty tree
if not root:
return res
level = [root]
while level:
temp = []
res.insert(0, [n.val for n in level])
for node in level:
if node.left:
temp.append(node.left)
if node.right:
temp.append(node.right)
level = temp
# Return
return res

【LeetCode OJ】Binary Tree Level Order Traversal II的更多相关文章

  1. 【LeetCode OJ】Binary Tree Level Order Traversal

    Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal/ Traverse the tree ...

  2. LeetCode OJ 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 ...

  3. LeetCode OJ: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】【Easy】Binary Tree Level Order Traversal II

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

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

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

  7. LeetCode OJ 102. Binary Tree Level Order Traversal

    题目 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to rig ...

  8. LeetCode OJ:Binary Tree Level Order Traversal(二叉树的层序遍历)

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

  9. LeetCode算法题-Binary Tree Level Order Traversal II(Java实现)

    这是悦乐书的第165次更新,第167篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第24题(顺位题号是107).给定二叉树,返回其节点值的自下而上级别顺序遍历(即从左到右 ...

随机推荐

  1. python(五)文件操作

    1.打开文件 f = open('db','r')   #只读 f = open('db','w')   #只写,先清空原文件 f = open('db','x')   #文件存在,报错,不存在,创建 ...

  2. C++开源大全

    程序员要站在巨人的肩膀上,C++拥有丰富的开源库,这里包括:标准库.Web应用框架.人工智能.数据库.图片处理.机器学习.日志.代码分析等. 标准库 C++ Standard Library:是一系列 ...

  3. ArcGIS Javascript地图上添加json数据格式的点

    /** * 显示地图点. * json的格式[{"name":"name1","x":"x1","y" ...

  4. python学习总结1

    1.python环境搭建 1.下载python并安装,下载地址:http://pan.baidu.com/s/1jHpWblk 2.启动idle即可编辑python代码 2.基本语法 1.注释:单行注 ...

  5. Oracle子查询(嵌套查询)

    概念: 所谓子查询,即一个select语句中嵌套了另外的一个或者多个select语句 需求:查找和Smith同部门的所有员工的id和last_name 目标: 员工id,last_name from: ...

  6. 简单的SpringMVC的测试项目----跟struts2对比着学习

    一,搭建SpringMVC项目 1,主配置文件-----地基 because,不像struts2是个独立的框架,可以脱离Spring单独使用.需要先添加Spring支持,是Spring框架组成的一部分 ...

  7. bzoj 4415: [Shoi2013]发牌

    4415: [Shoi2013]发牌 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 173  Solved: 124[Submit][Status][ ...

  8. sublime text3 快捷方式汇总

    sublime text. 用过的都给赞, 哈哈-- 下面是快捷方式汇总啦: 选择类: Ctrl+D 选中光标所占的文本,继续操作则会选中下一个相同的文本. Alt+F3 选中文本按下快捷键,即可一次 ...

  9. tomcat下iims的配置感悟

    1.没有想(意识)到清楚:resin下的web.xml 和tomcat下的web.xml是不同的. 2.对于connect读取配置文件以及连接数据库根本就没有意识. /** * 获取本系统DB配置的文 ...

  10. ssh框架介绍

    SSH 为 struts+spring+hibernate 的一个集成框架,是目前较流行的一种JAVA Web应用程序开源框架. Struts Struts是一个基于Sun J2EE平台的MVC框架, ...