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

For example:
Given binary tree [3,9,20,null,null,15,7],

    3
/ \
9 20
/ \
15 7

return its level order traversal as:

[
[3],
[9,20],
[15,7]
] 这个题目非常明显的适用BFS, 从root, 依次将每一层append进入ans里, 只是要注意的就是如果判断哪个是在哪一层呢, 这里用到的方法是利用size of queue, 每次得到的size就是该层的数目,
每层用一个temp list去存每一层的元素, 结束之后append进入ans中. 1. Constraints
1) tree 可以为empty, 所以edge case 为root == None 2. Ideas BFS T: O(n) S: O(n) n is number of nodes in the tree 3. code
import collections
class Solution:
def levelOrder(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
if not root: return []
ans, queue = [], collections.deque([root])
while queue:
size, level = len(queue), []
for _ in range(size):
node = queue.popleft()
level.append(node.val)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
ans.append(level)
return ans

4. Test cases

1) None   =>   []

2) [1]    =>  [[1]]

3)

Given binary tree [3,9,20,null,null,15,7],

    3
/ \
9 20
/ \
15 7

return its level order traversal as:

[
[3],
[9,20],
[15,7]
]

[LeetCode] 102. Binary Tree Level Order Traversal_Medium tag: BFS的更多相关文章

  1. leetcode 102 Binary Tree Level Order Traversal(DFS||BFS)

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

  2. Leetcode 102 Binary Tree Level Order Traversal 二叉树+BFS

    二叉树的层次遍历 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * ...

  3. [LeetCode] 102. Binary Tree Level Order Traversal 二叉树层序遍历

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

  4. leetcode 102. Binary Tree Level Order Traversal

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

  5. leetcode 102 Binary Tree Level Order Traversal ----- java

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

  6. Java [Leetcode 102]Binary Tree Level Order Traversal

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

  7. Leetcode 102. Binary Tree Level Order Traversal(二叉树的层序遍历)

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

  8. LeetCode 102. Binary Tree Level Order Traversal 二叉树的层次遍历 C++

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

  9. Java for LeetCode 102 Binary Tree Level Order Traversal

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

随机推荐

  1. 二叉树的实现(Java语言描述)

    实现二叉树   并先序遍历之. package 二叉树的实现; public class BinaryTree<T> { class Node { int value; // 该节点存储的 ...

  2. js 的空值判断程序

    function empty(v){ switch (typeof v){ case 'undefined' : return true; case 'string' : if($.trim(v).l ...

  3. [转]OpenStack Neutron运行机制解析概要

    转载自:http://panpei.net.cn/2013/12/04/openstack-neutron-mechanism-introduce/ 自从开学以来,玩OpenStack也已经3个月了, ...

  4. cheerio ==> node中的jquery

    三.cheerio ==> node中的jquery  https://www.npmjs.com/package/cheerio $ npm install cheerio -- save 1 ...

  5. 使用jvisuamvm的btrace插件

    在之前的文章中写了如何使用btrace来监控运行中的jvm的方法的参数和返回值 jvisualvm中提供了一个btrace插件,我们可以更方便地attach到一个运行中的jvm 更方便地执行和停止bt ...

  6. Setting up Unicorn with Nginx

    gem install unicorn or gem 'unciron' 1 install Nginx yum install ... 2 Configuration vi /etc/nginx/n ...

  7. 【CF799E】Aquarium decoration 线段树

    [CF799E]Aquarium decoration 题意:有n个物品,小A和小B各自喜欢其中的某些物品,一件物品可能既被小A喜欢又被小B喜欢,也可能既不被小A喜欢又不被小B喜欢.每个物品都有一个价 ...

  8. RabbitMQ安装详解(centos6.8)(转自:http://www.cnblogs.com/zhen-rh/p/6862350.html)

    1.下载rabbitmq安装包 2.安装erlang a.安装Erlang Solutions仓库到你的系统(目的在于让你可以使用yum安装到最新版本的erlang, 如果不设置, yum安装的erl ...

  9. python转化字符串形式的json

    在使用python对字符串形式的json进行 json.loads() 的操作时,遇到了:JSONDecodeError: Invalid \escape,提示\无法excape,可以通过以下方式将字 ...

  10. vue之单表输入绑定

    vue的核心:声明式的指令和数据的双向绑定. 那么声明式的指令,已经给大家介绍完了.接下来我们来研究一下什么是数据的双向绑定? 另外,大家一定要知道vue的设计模式:MVVM M是Model的简写,V ...