Problem Link:

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

Traverse the tree level by level using BFS method.

# 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 levelOrder(self, root):
"""
BFS from the root.
For each level, insert a list of values into the result
"""
res = []
if not root:
return res
q = [root]
while q:
new_q = []
res.append([n.val for n in q])
for node in q:
if node.left:
new_q.append(node.left)
if node.right:
new_q.append(node.right)
q = new_q
return res

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

  1. 【LeetCode OJ】Binary Tree Level Order Traversal II

    Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ Use BFS from th ...

  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 102. Binary Tree Level Order Traversal

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

  4. 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 ...

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

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

  7. 【leetcode】Binary Tree Level Order Traversal I & II

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

  8. 【题解】【BT】【Leetcode】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

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

随机推荐

  1. 网页中多媒体对像标记<OBJECT>和<EMBED>

    在网页中常用object和embed标记插入的多媒体对象,比如FLASH,视频等. 用一个快播例子说明下这两个标记的使用和区别: <object classid="clsid:F3D0 ...

  2. Java中如何克隆集合——ArrayList和HashSet深拷贝

    编程人员经常误用各个集合类提供的拷贝构造函数作为克隆List,Set,ArrayList,HashSet或者其他集合实现的方法.需要记住的是,Java集合的拷贝构造函数只提供浅拷贝而不是深拷贝,这意味 ...

  3. 关于javascript闭包中的this对象

    我们知道, this对象是运行时基于函数的执行环境绑定的:在全局函数中,this等于window,而当函数被作为某个对象的方法调用时,this等于那个对象.<Javascript高级程序设计&g ...

  4. .Net Globalization and Localization

    随着互联网的发展日益壮大和活跃,网上购物交易越来越频繁,一个网站支持多种语言在所难免,所以国际化和本地化在现在的网站中的作用越来越大,一个网站的使用量和搜索量有可能受国际化的影响一点.所以在当今做一个 ...

  5. 非对称加密算法--DH

    注意:本节内容主要参考自<Java加密与解密的艺术(第2版)>第8章“高等加密算法--非对称加密算法” 11.1.非对称加密算法 特点: 发送方和接收方均有一个密钥对(公钥+私钥),其中公 ...

  6. TabActivity 切换Activity界面

    TAB切换先上图,tab标题没有添加样式,因为setIndicator可以直接接收View,所以可以自己编辑样式: 也可以实现OnTabChangeListener监听tab的点击,改变tab点击后的 ...

  7. JAVA字符串的相关练习

    /*String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";//生成随机验证码 fo ...

  8. bzoj3504: [Cqoi2014]危桥

    题意:给出一个图,有的边可以无限走,有的只能走两次(从一头到另一头为一次),给定两个起点以及对应的终点以及对应要走几个来回,求判断是否能完成. 先来一个NAIVE的建图:直接限制边建为容量1,无限制为 ...

  9. java 中的SimpleDateFormat、Date函数以及字符串和Date类型互转

    SimpleDateFormat是一个以与语言环境有关的方式来格式化和解析日期的具体类.它允许进行格式化(日期 -> 文本).解析(文本 -> 日期)和规范化. SimpleDateFor ...

  10. Unity3d之个性化鼠标

    代码实例: using UnityEngine; using System.Collections; public class CursorController : MonoBehaviour { / ...