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 ...
随机推荐
- Java 分布式和集中式理解
文章转载自:https://blog.csdn.net/youanyyou/article/details/79406507
- moment.js返回本周
项目中需要做个打卡的模块.里面有个模块需要返回当前这个星期从星期日到星期六的日期,如下图: 我是通过 moment.js 的 moment().day() 实现这个效果的,它的说明如下图: 关于这个插 ...
- Java知多少(25)再谈Java包
在Java中,为了组织代码的方便,可以将功能相似的类放到一个文件夹内,这个文件夹,就叫做包. 包不但可以包含类,还可以包含接口和其他的包. 目录以"\"来表示层级关系,例如 E:\ ...
- Path expected for join!错误处理
在Hibernate中,进行连接查询时,如果使用join语句,就容易产生标题所示的错误. 比如from Content o left join Mcp mcp where o.mcp.id=mcp.i ...
- 弹出框插件——dialog
基于jquery和dot.js弹出框插件,兼容IE6+等其他浏览器. 思想:弹出框元素插入body节点中,并在页面垂直居中显示(fixed定位),触发确定和关闭事件绑定. 注意ie6包含两个问题:一. ...
- DataTable导出为word,excel,html,csv,pdf,.txt
using System; using System.Data; using System.Configuration; using System.Collections; using System. ...
- cordova 插件 调用iOS社交化分享(ShareSDK:微信QQ分享)
1.github上已有的插件:https://github.com/nwpuhmz/ShareSDKPlugin 2.安装插件 cordova plugin add https://github.co ...
- mui---获取上一级窗口
我们在用MUI做APP的时候,会用到要获取上一级的窗口.具体方法:获取当前webview窗口的创建者. 代码: plus.webview.currentWebview().opener();
- Python 读、写、追加csv文件详细以及注意事项
一.利用csv库创建文件 首先导入csv文件 import csv 根据指定的path创建文件: def create_csv(path): with open(path, "w+" ...
- mysql 登录远程数据库 失败
配置mysql结束,本机能够成功登录和查询. 但其他主机使用mysql -h*** -P3306 -u*** -p***,登录数据库. centos7上反馈:ERROR 2003 (HY000): C ...