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].
Note:
The range of node's value is in the range of 32-bit signed integer.
这道题描述的是:
给我们一颗完全二叉树,我们求出二叉树每层的节点平均值 我的思想:
对二叉树进行广度遍历,用一个二维数组存下每一层的所有节点
再对每一层所有节点进行求平均数 伪代码:
python中数组是动态的
用一个levels列表,里面每一个元素都是一个列表,列表里存着每层的所有节点
广度遍历的时候,动态生成下一个元素和下一层列表 1 levels = [[root] ] 根自己是第一层,levels里面
2 对levels 一个一个拿出里面的列表用level表示
(如果取出来的是空列表,说明到最后一层了,跳出循环)
2.1 当前列表level为一层,里面存着所有当前层元素
2.2 为levels追加一个空列表[] 用于存储下一层
2.3 一个一个取出level里面的元素node
如果 node有left,node.left追加到下一层列表
如果 node有right,node.right追加到下一层列表
2.4 计算当前层所有节点的平均值,追加大结果列表res 我的python代码:
 # Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def averageOfLevels(self, root):
"""
:type root: TreeNode
:rtype: List[float]
"""
levels = [[root]] #将要进行广度遍历,每一层新开一个列表,每个列表存着每层的节点
res = [ ] #用于存储每层的平均数
i = 0
while i < len(levels) and levels[i] != []:
level = levels[i]
j = 0
temp = 0 # 临时变量用于存储当前层的节点值加和
levels.append([]) #开启新的一层
while j < len(level):
node = level[j]
if node.left is not None:
levels[i+1].append(node.left)
if node.right is not None:
levels[i+1].append(node.right)
temp+=node.val
j += 1
res.append(temp/j)
i += 1
return res

leetcode算法: Average of Levels in Binary Tree的更多相关文章

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

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

  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. LeetCode 637. 二叉树的层平均值(Average of Levels in Binary Tree)

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

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

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

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

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

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

随机推荐

  1. 用user-selection实现让页面上的内容不能被选中

    最开始发现这个功能是在陌小雨的博客中,然后自己百度发现用的是user-selection功能,之前网上有很多关于禁止右键,禁止复制,禁止粘 贴,禁止剪切等都弱爆了.这个功能正好使用到我的网站上啊,(你 ...

  2. 自创最精简的python装饰器

    个人心血原创,欢迎转载,请注明作者和出处.否则依法追究法律责任!!!! author:headsen  chen date:2018-03-21  10:37:52 代码: 代码解析过程:1,def ...

  3. Linux上部署SVN

    Linux上部署SVN author:headsen chen  2017-10-16  16:45:04 前提:通过yum来安装,必须是centos6.5的桌面版的.否则会出现某些的安装包不全而导致 ...

  4. centos7服务的管理

    centos7上服务管理  author:headsen chen     2017-10-16  16:03:53       1,启动服务(每条都可以) systemctl start httpd ...

  5. Ubuntu14.04上修改主机名

    Ubuntu14.04上修改主机名 author:headsen chen  2017-10-12 15:41:31 个人原创,转载请注明作者,出处,否则依法追击法律责任 查看主机名:hostname ...

  6. 使用kafka connect,将数据批量写到hdfs完整过程

    版权声明:本文为博主原创文章,未经博主允许不得转载 本文是基于hadoop 2.7.1,以及kafka 0.11.0.0.kafka-connect是以单节点模式运行,即standalone. 首先, ...

  7. Learn Plan

    2018-02-05 1.正则表达式的熟悉和使用 2.Spring-boot 框架的使用 3.React的如何和使用 4.英语的单词发音的纠正和词汇的记忆. 5.github 命令行的使用 6.jav ...

  8. 用js写的时钟Demo

    css代码: <style type="text/css"> .a { width: 200px; height: 100px; position: absolute; ...

  9. Maven-11: 从命令行调用插件

    mvn -h显示mvn命令帮助: usage: mvn [options] [<goal(s)>] [<phase(s)>] Options: -am,--also-make ...

  10. “Swift Language Version” (SWIFT_VERSION) build setting must be set to a supported value for targets which use Swift

    使用cocopod导入第三方swift包后,编译报以下错误: The "Swift Language Version" (SWIFT_VERSION) build setting ...