"""
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]
] """
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None class Solution:
def levelOrder(self, root):
if root == None: #树为空,直接返回[]
return []
queue = [root]
cur = []
res = []
while queue:
cur = [x.val if x else None for x in queue] #存当前层的value
newqueue = [] #newqueue用来遍历下一层使用
for x in queue:
if x.left: #bug 如果不判断,结果包含[None]
newqueue.append(x.left)
if x.right:
newqueue.append(x.right)
queue = newqueue
res.append(cur) #将当前[]append到最后结果
return res
"""
本题与leetcode101相似,用BFS可解
"""

leetcode102 Binary Tree Level Order Traversal的更多相关文章

  1. 剑指offer从上往下打印二叉树 、leetcode102. Binary Tree Level Order Traversal(即剑指把二叉树打印成多行、层序打印)、107. Binary Tree Level Order Traversal II 、103. Binary Tree Zigzag Level Order Traversal(剑指之字型打印)

    从上往下打印二叉树这个是不分行的,用一个队列就可以实现 class Solution { public: vector<int> PrintFromTopToBottom(TreeNode ...

  2. LeetCode102 Binary Tree Level Order Traversal Java

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

  3. (二叉树 BFS) leetcode102. Binary Tree Level Order Traversal

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

  4. 32-2题:LeetCode102. Binary Tree Level Order Traversal二叉树层次遍历/分行从上到下打印二叉树

    题目 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 ...

  5. Leetcode102. Binary Tree Level Order Traversal二叉树的层次遍历

    给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回其 ...

  6. (二叉树 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 ...

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

  8. [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 I & II

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

随机推荐

  1. python爬虫(七) mozillacookiejar

    MozillaCookiejar 保存百度得Cookiejar信息: from urllib import request from urllib import parse from http.coo ...

  2. UCOS-III API函数

    附录:UCOS-III API函数 任务管理 就绪列表 挂起队列 时间管理 信号量 消息队列 内存管理

  3. [经验] SpringBoot 远程连接 Linux 上的 Redis

    开发环境: ---------- springboot 2.X ---------- Linux Ubuntu 18.0.04 关于怎么在 Ubuntu 上安装 Linux , 网上的教程一大堆, 这 ...

  4. ardrino#串口控制led

    void setup() { pinMode(D6, OUTPUT); digitalWrite(D6,HIGH); Serial.begin(); } void loop() { String st ...

  5. 「CH6801」棋盘覆盖

    「CH6801」棋盘覆盖 传送门 考虑将棋盘黑白染色,两个都无障碍的相邻的点之间连边,边的容量都为1,然后就求一次最大匹配即可 参考代码: #include <cstring> #incl ...

  6. Django 学习组件分页器与自定制分页器

    一.Django 分页器 1.django的分页器基础版 (1)首先是基础数据分别为 from django.db import models # Create your models here. c ...

  7. Django 学习之From组件

    一.Form组件介绍 Form组件可以做的几件事情: 1.用户请求数据验证 2.自动生成错误信息 3.打包用户提交的正确信息 4.如果其中有一个错误了,其他的正确这,保留上次输入的内容 4.自动创建i ...

  8. SpringBoot 集成Spring JDBC

    (1)在pom.xml中添加依赖 <!--spring-jdbc的依赖--> <dependency> <groupId>org.springframework.b ...

  9. Pandas 用法汇总

    一.生成数据表 1.首先导入pandas 库,一般会用到 numpy 库,所以我们先导入备用: import numpy as np import pandas as pd 2.生成 CSV 或者 x ...

  10. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...