问题描述:

Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array.

Example 1:

Input:
3
/ \
9 20
/ \
15 7
Output: [3, 14.5, 11]
Explanation:
The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11. Hence return [3, 14.5, 11].

思路:

考虑BFS算法,因为这是第一次碰到BFS算法,因而将该题记录

代码:

 # Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
def averageOfLevels(self, root: TreeNode) -> List[float]:
result = []
self.BFS(root,result)
return result def BFS(self,root,result):
if root == None: return
queue = [root]
while queue:#如果queue不为空
val = [i.val for i in queue if i]
if len(val):result.append(sum(val)/len(val))
queue = [child for node in queue if node for child in (node.left,node.right)]

BFS的基本思路是:将每一层的结点放置于一个list中,然后遍历List对每个结点进行相应操作,下一步更新该list,将该list放置更下一层的结点。该算法不用递归调用自身,相对而言理解比较容易

Python3解leetcode Average of Levels in Binary Tree的更多相关文章

  1. [LeetCode] Average of Levels in Binary Tree 二叉树的层平均值

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

  2. LeetCode 637. 二叉树的层平均值(Average of Levels in Binary Tree)

    637. 二叉树的层平均值 637. Average of Levels in Binary Tree LeetCode637. Average of Levels in Binary Tree 题目 ...

  3. 637. Average of Levels in Binary Tree - LeetCode

    Question 637. Average of Levels in Binary Tree Solution 思路:定义一个map,层数作为key,value保存每层的元素个数和所有元素的和,遍历这 ...

  4. 【Leetcode_easy】637. Average of Levels in Binary Tree

    problem 637. Average of Levels in Binary Tree 参考 1. Leetcode_easy_637. Average of Levels in Binary T ...

  5. leetcode算法: Average of Levels in Binary Tree

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

  6. [LeetCode] 637. Average of Levels in Binary Tree 二叉树的层平均值

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

  7. 【LeetCode】637. Average of Levels in Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...

  8. LeetCode算法题-Average of Levels in Binary Tree(Java实现)

    这是悦乐书的第277次更新,第293篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第145题(顺位题号是637).给定一个非空二叉树,以数组的形式返回每一层节点值之和的平 ...

  9. LeetCode 637 Average of Levels in Binary Tree 解题报告

    题目要求 Given a non-empty binary tree, return the average value of the nodes on each level in the form ...

随机推荐

  1. python使用__new__创建一个单例模式(单例对象)

    #单例模式:使一个类只产生一个对象.他们的id地址都指向同一个内存地址 第一步:理解谁创建了对象 # 单例模式# 首先明白,我们在创建一个类的对象的时候,其实是调用的这个类的父类,即继承object, ...

  2. 20160419—JS备忘:服务器回发刷新页面提示重试的解决方案。

    有事页面刷新时 提示如下: js使用的是:location.reload()的刷新方式. 使用js重新定向该页面:location.href="a.aspx"; 当使用:self. ...

  3. linux(centos6.5)常用命令

    前言:由于项目项目使用的是linux服务器,因此会使用到较多linux命令,本文对centos下常用命令进行记录 1.vi的三种模式 2.解压缩相关 3.用户相关 4.文件相关 5.各种查看命令 1. ...

  4. 20191105 《Spring5高级编程》笔记-第12章

    第12章 使用Spring远程处理 12.4 在Spring中使用JMS 使用面向消息的中间件(通常成为MQ服务器)是另一种支持应用程序间通信的流行方法.消息队列(MQ)服务器的主要优点在于为应用程序 ...

  5. 前端 CSS的选择器 基本选择器 类选择器

    类选择器 符号是.开头 然后类的名字 样式类名不要用数字开头(有的浏览器不认). 所谓类就是class,.class与id非常相似,任何标签都可以加类,但是类可以重复 通过样式类选择元素: 示例: & ...

  6. ESP32、GPRS A9测试

    测试内容: 1.A9作为客户端,在服务器主动断开连接或异常断开的时候,使用网络连接状态查询接口,能否获得准确的网络连接状态. 结果: TCP: A9开多连接时,成功连接TCP服务器后,发送查询语句AT ...

  7. Java JDK在Mac下的配置方法

    Java JDK在Mac.Windows下的配置方法 Mac 第一步:下载JDK 官网下载地址 第二步:安装JDK 安装步骤很简单,一直点击下一步即可. 第三步:配置环境变量 打开terminal(终 ...

  8. H-Updating a Dictionary (模拟)

    In this problem, a dictionary is collection of key-value pairs, where keys are lower-case letters, a ...

  9. 基本SQL查询语句

    使用Emp表和Dept表完成下列练习 Emp员工表 empno ename job Mgr Hiredate Sal Comm Deptno 员工号 员工姓名 工作 上级编号 受雇日期 薪金 佣金 部 ...

  10. Java JNA (五)—— 释放Memory对象分配的内存

    Java进程的内存包括Java NonHeap空间.Java Heap空间和Native Heap空间. JNA中的Memory对象是从Native Heap中分配空间.但java的GC是针对Java ...