【LeetCode OJ】Binary Tree Level Order Traversal II
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的更多相关文章
- 【LeetCode OJ】Binary Tree Level Order Traversal
Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal/ Traverse the tree ...
- 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 ...
- 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 ...
- 【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 ...
- 【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 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 ...
- 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, ...
- LeetCode算法题-Binary Tree Level Order Traversal II(Java实现)
这是悦乐书的第165次更新,第167篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第24题(顺位题号是107).给定二叉树,返回其节点值的自下而上级别顺序遍历(即从左到右 ...
随机推荐
- shell & dialog
最近使用dialog写图形自动化shell脚本, 功能很强大,功能不是非常多但是足够用.想写一篇linux下dialog的使用方法,虽然命令不多,但是写起来也需要下很大功夫,而且不一定写得更好,在网 ...
- [问题2015S13] 复旦高等代数 II(14级)每周一题(第十四教学周)
[问题2015S13] 设 \(A=(a_{ij})\) 为 \(n\) 阶实矩阵, 定义函数 \[f(A)=\sum_{i,j=1}^na_{ij}^2.\] 设 \(P\) 为 \(n\) 阶非 ...
- EasyUI关于 numberbox,combobox,validatebox 的几个小问题
在最近的项目中,首次使用到了 网页的一个布局框架——EasyUI,感觉这个框架特别牛,兼容性很不错,页面效果也挺不错,可是在使用标题上三个控件过程中遇到几个很奇特的问题,让我头疼不已,所以在此给广大I ...
- jquery总结04-DOM节点操作
一般js操作节点 ①创建节点(元素文本)document.createElement innerHTML ②添加属性 setAttribute ③加入文档 appendChild 操作繁琐还有兼容性 ...
- angular directive scope
angular directive scope 1.当directive 中不指定scope属性,则该directive 直接使用 app 的scope: 2.当directive 中指定scope属 ...
- stm32cube--IWDG使用
IWDG使用的是32芯片内部的40k独立晶振,该晶振为rtc和iwdg提供时钟,即使是主时钟坏了也不影响它们. 主要用到三个寄存器, IWDG_KR 键值寄存器 IWDG_PR 预分频寄 ...
- 谷歌 火狐 CSS兼容
@media screen and (-webkit-min-device-pixel-ratio:0){}谷歌@-moz-document url-prefix(){}火狐
- maven设置
<localRepository>D:/apps/eclipse/env/maven/repository</localRepository>
- 转:PHP--获取响应头(Response Header)方法
转:http://blog.sina.com.cn/s/blog_5f54f0be0102uvxu.html PHP--获取响应头(Response Header)方法 方法一: ========== ...
- 用Gen8服务器来学习虚拟化ESXI
虚拟化和云计算是这几年的热门技术,VMware公司是虚拟化的领头羊,小坦克我有幸在VMware从事这方面的测试工作. 本系列将会讲述我学习虚拟化的一些经历. 将会覆盖一些虚拟化产品: 比如:VMwar ...