题目要求

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

题目分析及思路

给定一棵非空二叉树,要求以列表的形式返回每一层结点值的平均值。可以使用队列保存结点,进行层次遍历,要特别注意空结点的判断。

python代码

# 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]:

aves = []

q = collections.deque()

q.append(root)

while q:

level = []

size = len(q)

count = 0

for _ in range(size):

node = q.popleft()

if not node:

count += 1

continue

level.append(node.val)

q.append(node.left)

q.append(node.right)

if size-count != 0:

aves.append(sum(level)/(size-count))

return aves

LeetCode 637 Average of Levels in Binary Tree 解题报告的更多相关文章

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

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

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

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

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

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

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

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

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

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

  8. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

  9. 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)

    [LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...

随机推荐

  1. 基于CSS3动态背景登录框代码

    基于CSS3动态背景登录框代码.这是一款基于jQuery+CSS3实现的带有动画效果的动态背景登陆框特效.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div class ...

  2. 【iCore4 双核心板_ARM】例程十一:DMA实验——存储器到存储器的传输

    实验原理: DAM(直接存储器访问)传输不需要占用CPU,可以在存储器至存储器实现高速的数据 传输.本实验采用DAM2控制器的数据流0,选用通道0进行数据传输.通过LED的颜色来 判断传输是否成功. ...

  3. AtomicInteger 源码阅读

    Package java.util.concurrent.atomic 这是一个小工具包,它的实际作用是提供了很多个无阻塞的线程安全的变量操作工具. 无阻塞的线程安全:其含义就是不使用 synchro ...

  4. Java知多少(58)线程Runnable接口和Thread类详解

    大多数情况,通过实例化一个Thread对象来创建一个线程.Java定义了两种方式: 实现Runnable 接口: 可以继承Thread类. 下面的两小节依次介绍了每一种方式. 实现Runnable接口 ...

  5. USB学习笔记连载(二十一):CY7C68013A进行数据传输(一)

    官方手册中给出了bulkloop参考例程,此例程是PC从端口2发送出数据,然后从端口6接收到数据,那么根据这个思想,可以进行修改,使得PC机接收到的数据不是从EP2发送过来的,而是从外部逻辑,比如FP ...

  6. python一天一题(3)

    #--coding=utf8-- from selenium import webdriver import time import logging import os.path ''' 搜索取搜索的 ...

  7. 手动升级11.2.0.1的rac数据库到11.2.0.4

    ① 关闭两个节点上的数据库 crsctl stop resource ora.ORA11G.db ② 命令行单节点启动数据库, 注意这里的SQLPLUS 一定是升级后的软件地址 sqlplus / a ...

  8. 基于Java的数据采集(二)

    在上一篇文章<基于Java的数据采集(一)>:http://www.cnblogs.com/lichenwei/p/3904715.html 提到了如何如何读取网页源代码,并通过group ...

  9. Implementing HTTPS Everywhere in ASP.Net MVC application.

    Implementing HTTPS Everywhere in ASP.Net MVC application. HTTPS everywhere is a common theme of the ...

  10. 简单的SSM框架搭建教程

    简单的ssm框架的搭建和配置文件 ssm框架里边的配置: 1.src路径下直接存放数据库和log4j的properties文件 2.src路径下建个config包,分别放置ssm的xml文件 3.修改 ...