Python3解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].
思路:
考虑BFS算法,因为这是第一次碰到BFS算法,因而将该题记录
代码:
# 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]:
result = []
self.BFS(root,result)
return result def BFS(self,root,result):
if root == None: return
queue = [root]
while queue:#如果queue不为空
val = [i.val for i in queue if i]
if len(val):result.append(sum(val)/len(val))
queue = [child for node in queue if node for child in (node.left,node.right)]
BFS的基本思路是:将每一层的结点放置于一个list中,然后遍历List对每个结点进行相应操作,下一步更新该list,将该list放置更下一层的结点。该算法不用递归调用自身,相对而言理解比较容易
Python3解leetcode Average of Levels in Binary Tree的更多相关文章
- [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 ...
- 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
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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...
- LeetCode算法题-Average of Levels in Binary Tree(Java实现)
这是悦乐书的第277次更新,第293篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第145题(顺位题号是637).给定一个非空二叉树,以数组的形式返回每一层节点值之和的平 ...
- 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 ...
随机推荐
- 007-Spring Boot-@Enable*注解的工作原理-EnableConfigurationProperties、ImportSelector、ImportBeanDefinitionRegistrar
一.@Enable* 启用某个特性的注解 1.EnableConfigurationProperties 回顾属性装配 application.properties中添加 tomcat.host=19 ...
- Delphi XE2 之 FireMonkey 入门(25) - 数据绑定: TBindingsList: 表达式的灵活性及表达式函数
Delphi XE2 之 FireMonkey 入门(25) - 数据绑定: TBindingsList: 表达式的灵活性及表达式函数 绑定表达式中可以有简单的运算和字符串连接, 但字符串需放在双引号 ...
- UDP 首部的格式
<图解TCP/IP>6.6 UDP首部的格式 源端口号:表示发送端端口号,字段长16位.该字段是可选项,有时可能不会设置源端口号.没有源端口号的时候该字段的设置为0.可用于不需要返回的通信 ...
- JAVA在页面查看或下载服务器上的日志
1.配置 FileUtils类所需jar包的maven地址 <dependency> <groupId>commons-io</groupId> <artif ...
- C# datatable 与 xml文件之间的转换
/// <summary> /// datatable转XML文件 /// </summary> /// <param name="dtTable"& ...
- sqoop应用
1.导入数据(将mysql(rdbms)的表的数据导入到hdfs) 1.1.全部导入(注意空格) sqoop import \ --connect jdbc:mysql://192.168.159.1 ...
- Common Linux Commands 日常工作常用Linux命令
How to know CPU info cat /proc/cpuinfo arch How to know memory info: cat /proc/meminfo ...
- java如何台生成二维码详解
现在呢说明页面上展示二维码的两种方式: 1.使用img标签的src来请求生成二维码,后台会直接返回: 2.此处跟上方意思相似,获取到url给img标签设置src属性: 特别注意:如果url有amp;, ...
- js之状态模式
level01:电灯程序 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...
- HDU-1269 迷宫城堡(连通分量)
迷宫城堡 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...