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 ...
随机推荐
- JS中的Map和Set实现映射对象
使用iterable内置的forEach方法 var a = ['A', 'B', 'C']; a.forEach(function (element, index, array) { // elem ...
- [ModemManger]ModemManger的取消
http://www.linux-databook.info/?page_id=3748 systemctl disable ModemManager.service 失能ModemManger从而取 ...
- 怎么关闭win10和win8快速启动
电源选项-- 选择电源按钮的功能--- 更改当前不可用的设置-- 快速启动勾去掉
- UML类图关系大全【转】
UML类图关系大全 1.关联 双向关联:C1-C2:指双方都知道对方的存在,都可以调用对方的公共属性和方法. 在GOF的设计模式书上是这样描述的:虽然在分析阶段这种关系是适用的,但我们觉得它对于描述设 ...
- 第四百节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署CentOS6.5安装python3.5.1
第四百节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署CentOS6.5安装python3.5.1 1.检查系统是否安装了python [root@192 ~]# rpm -qa ...
- ViewPager PagerAdapter not updating the View
There are several ways to achieve this. The first option is easier, but bit more inefficient. Overri ...
- C#操作Word Aspose.Words组件介绍及使用—基本介绍与DOM概述
1.基本介绍 Aspose.Words是一个商业.NET类库,可以使得应用程序处理大量的文件任务.Aspose.Words支持Doc,Docx,RTF,HTML,OpenDocument,PDF,XP ...
- 【代码审计】五指CMS_v4.1.0 后台存在SQL注入漏洞分析
0x00 环境准备 五指CMS官网:https://www.wuzhicms.com/ 网站源码版本:五指CMS v4.1.0 UTF-8 开源版 程序源码下载:https://www.wuzhi ...
- 《Python3网络爬虫开发实战》
推荐:★ ★ ★ ★ ★ 第1章 开发环境配置 第2章 网页基础知识 第3章 网络爬虫基础 第4章 基本库的使用 第5章 解析库的使用 第6章 数据存储 第7章 Ajax数据爬取 第8章 动态渲染页面 ...
- JSON的多种转换
String message = httpSend(url, empName, loginPassWd); // 解析json字符串 message = message.replaceAll(&quo ...