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 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 解题报告的更多相关文章
- 【LeetCode】637. Average of Levels in Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...
- [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二叉树的层平均值 (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 ...
- 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】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...
随机推荐
- Android Studio原生库创建示例
[时间:2017-07] [状态:Open] [关键词:Android,Android Studio,gradle,native,c,c++,cmake,原生开发] 0 引言 最近在工作中遇到了升级A ...
- CentOS7安装RabbitMQ
1.先安装Erlang rpm -Uvh http://www.rabbitmq.com/releases/erlang/erlang-18.1-1.el7.centos.x86_64.rpm 2.安 ...
- yum只下载软件不安装的两种方法
1 通过yum自带一个工具:yumdownloader rpm -qa |grep yum-utils yum -y install yum-utils* rpm -ql yum-utils 安装好后 ...
- JVM:从实际案例聊聊Java应用的GC优化
原文转载自美团从实际案例聊聊Java应用的GC优化,感谢原作者的贡献 当Java程序性能达不到既定目标,且其他优化手段都已经穷尽时,通常需要调整垃圾回收器来进一步提高性能,称为GC优化.但GC算法复杂 ...
- 解决:github上传时出现error: src refspec master does not match any
原因分析 引起该错误的原因是,目录中没有文件,空目录是不能提交上去的 解决方法 touch README git add README git commit -m 'first commit' git ...
- linux安装中文输入法
CentOS英文系统安装中文输入法,简单说说在CentOS 6.3下用yum安装中文输入法的过程. 1.需要root权限,所以要用root登录 ,或su root 2.yum install &q ...
- 【NPM】设置代理
https://yutuo.net/archives/c161bd450b2eaf88.html npm config set proxy http://server:port npm config ...
- jdk 自带的数据库Derby使用
ij是derby自带的一个功能强大的数据库管理工具,可以进行很多数据库管理的操作,包括创建数据库, 启动/关闭数据库,执行SQL脚本等.完成准备工作后,就可以启动并使用ij工具了. 在cmd中输入如下 ...
- 【中间件安全】Nginx 安全加固规范
1. 适用情况 适用于使用Nginx进行部署的Web网站. 2. 技能要求 熟悉Nginx配置,能够Nginx进行部署,并能针对站点使用Nginx进行安全加固. 3. 前置条件 1. 根据站点开放端口 ...
- Markdown 标题
用 Markdown 书写时,只需要在文本前面加上 # 即可创建标题,Markdown 支持六级标题,语法及效果如下 # 一级标题 ## 二级标题 ### 三级标题 #### 四级标题 ##### 五 ...