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 ...
随机推荐
- leetcode 40. 组合总和 II (python)
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...
- Java 八大基本数据类型
相关信息获取: (1)最小值:包装类.MIN_VALUE,如 Integer.MIN_VALUE (2)最大值:包装类.MAX_VALUE,如 Integer.MAX_VALUE (3)二进制位数:包 ...
- element-ui走马灯如何实现图片自适应 长度和高度 自适应屏幕大小
最近写用vue2.0写一个项目,用到了走马灯效果,由于项目赶时间,想偷下懒,第一次引用了element组件(纯JS也可以写的出来,赶时间嘛,懂得....),结果用了发现一个问题,element的组件( ...
- 《计算机程式设计》Week3 课堂笔记
本笔记记录自 Coursera课程 <计算机程式设计> 台湾大学 刘邦锋老师 Week3 Array 3-1 Array Usage 例子:使用数组一次申明10个整数变量 int a[10 ...
- layui框架中layer父子页面交互的方法分析
本文实例讲述了layui框架中layer父子页面交互的方法.分享给大家供大家参考,具体如下: layer是一款近年来备受青睐的web弹层组件,官网地址是:http://layer.layui.com/ ...
- 安全运维 - Linux系统维护
命令相关 帮助信息命令:help.whatis.info.which.whereis.man 目录管理: cd.ls.mkdir.rm.chmod.mv 用户管理: groupadd.groupdel ...
- JavaSE编码试题强化练习6
1.写出选择排序的代码实现,对一个int数组进行排序 public class TestSelectSort { public static void main(String[] args) { in ...
- AngularJs——基础小知识(二)
AngularJs的过滤器 1.Currency :过滤器(金额货币格式化)
- quartz任务调度的详解
1.Quartz包含3个核心(调度器.作业类.触发器) (1).作业类:只需要实现org.quartz.job接口,同时包含里面的一个方法体execute()[这是被调度的作业体] (2).调度器:是 ...
- html/css中BFC的开启、关闭、作用
什么是BFC BFC是什么并不重要.重要的是开启它干嘛?以及如何开启它 根据W3C的标准,在页面中元素都一个隐含的属性叫做Block Formatting Context(块级 格式化 环境)简称BF ...