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 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的更多相关文章
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- LeetCode 637. 二叉树的层平均值(Average of Levels in Binary Tree)
637. 二叉树的层平均值 637. Average of Levels in Binary Tree LeetCode637. Average of Levels in Binary Tree 题目 ...
- 637. Average of Levels in Binary Tree - LeetCode
Question 637. Average of Levels in Binary Tree Solution 思路:定义一个map,层数作为key,value保存每层的元素个数和所有元素的和,遍历这 ...
- 【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 ...
- LeetCode算法题-Average of Levels in Binary Tree(Java实现)
这是悦乐书的第277次更新,第293篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第145题(顺位题号是637).给定一个非空二叉树,以数组的形式返回每一层节点值之和的平 ...
随机推荐
- 纯代码实现WordPress评论回复自动添加@评论者的功能
先看看效果: 这个有什么用呢?添加了@功能之后那些用户评论之间的层次关系就很清晰了,我们可以清楚地知道这些评论是谁发给谁的. 其实主要是为了提升逼格. 实现方法: 将下面代码加入function.ph ...
- 如何写出如散文般的代码――《代码整洁之道》读书笔记(Ch1-Ch3)
不知道有多少人像我一样,程序出现问题时添加函数添加变量解决,变量名用a,b,c等"简单"的字母来表示.不知道有多少人像我一样,看完自己的代码,心里暗骂"什么玩意儿!&qu ...
- [UWP]理解ControlTemplate中的VisualTransition
1. 前言 VisualTransition是控件模板中的重要组成部分,无论是自定义控件或者修改控件样式都会接触到VisualTransition.明明这么重要,博客园上好像都没多少关于VisualT ...
- JS面向对象与面向过程
前言 面向对象编程: 就是将你的需求抽象成一个对象,然后针对这个对象分析其特征(属性)与动作(方法)--这个对象就称之为类 面向过程编程: 特点:封装,就是将你需要的功能放在一个对象里面 ------ ...
- django初探-创建简单的博客系统
django第一步 1. django安装 pip install django print(django.get_version()) 查看django版本 2. 创建项目 打开cmd,进入指定目录 ...
- java基础学习系列一
判断语句 1,if条件判断语句 if(){} if(){} else{} if(){}else if(){} else() if判断一共这3张情况.if后面的值是boolen类型 2,switch条件 ...
- FxCop卸载后依然生成文件夹的问题
在 http://www.cnblogs.com/heroius/p/8270004.html 中,通过编辑csproj文件可以移除对旧版nuget独立程序的依赖. 实际上,通过编辑项目文件的方式可以 ...
- MYSQL数据库学习七 视图的操作
7.1 视图 视图使程序员只关心感兴趣的某些特定数据和他们所负责的特定任务.提高了数据库中数据的安全性. 视图的特点如下: 视图的列可以来自不同的表,是表的抽象和在逻辑意义上建立的新关系. 视图是由基 ...
- Sping Boot入门到实战之实战篇(一):实现自定义Spring Boot Starter——阿里云消息队列服务Starter
在 Sping Boot入门到实战之入门篇(四):Spring Boot自动化配置 这篇中,我们知道Spring Boot自动化配置的实现,主要由如下几部分完成: @EnableAutoConfigu ...
- java使用io创建文件与删除文件的工具类
java中对于文件的操作,是再常见不过了.以下代码是自己代码中所用到的工具类,仅供参考. import java.io.File; import java.io.IOException; /** * ...