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. 设计模式——中介者模式/调停者模式(C++实现)

    #include <iostream> #include <string> using namespace std; class Colleague; class Mediat ...

  2. nginx location匹配顺序及CI框架的nginx配置

    Nginx location匹配顺序如下: 用前缀字符串定义的location规则对URI进行匹配测试. =号定义了精确的前缀字符串匹配,如果发现精确匹配则使用当前规则.否则继续下一步匹配. 匹配其它 ...

  3. sklearn包中有哪些数据集你都知道吗?

    注册了博客园一晃有3个月了,同时接触机器学习也断断续续的算是有1个月了.今天就用机器学习神器sklearn包的相关内容作为我的开篇文章吧. 本文将对sklearn包中的数据集做一个系统介绍,并简单说一 ...

  4. 移动web开发之rem的使用

    为什么要使用rem 移动端设备尺寸五花八门,单纯使用px这个单位无法轻易适配,rem就可以为我们解决这个问题! 如何使用rem 1rem默认等于16px,这是因为页面的默认字体大小就是16px.r 代 ...

  5. java 单向链表实现

    1 class Node{//Node类 2 private String data; 3 private Node next; 4 public Node(String data){ 5 this. ...

  6. postman简单教程,使用tests模块来验证接口时是否通过

    接口测试醉重要的就是返回数据的检查,一个简单的接口,我们可以肉眼检查返回数据,但接口一旦多起来且复杂,每次的检查都会很费劲,此时我们就需要postman 的tests模块来代替 概念: Postman ...

  7. Tomcat服务器的常用配置

    1.如何修改端口号, tomcat启动后经常会报端口冲突, 怎么办 如果部署在Linux环境下面, 首先使用netstat -apn命令检查是否是真的端口已经被占用了 如果真的被占用,进入tomcat ...

  8. 语句in

    Python :in在for中: for name  in names: names='1','2','3','4','5' for name in names: print(names) in no ...

  9. 用python程序来画花

    from turtle import * import time setup(600,800,0,0) speed(0) penup() seth(90) fd(340) seth(0) pendow ...

  10. JavaWeb学习笔记五 会话技术Cookie&Session

    什么是会话技术? 例如网站的购物系统,用户将购买的商品信息存储到哪里?因为Http协议是无状态的,也就是说每个客户访问服务器端资源时,服务器并不知道该客户端是谁,所以需要会话技术识别客户端的状态.会话 ...