作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/submissions/detail/136579829/

题目描述

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].

Note:

  1. The range of node’s value is in the range of 32-bit signed integer.

题目大意

求二叉树每层的所有节点的平均值

解题方法

方法一:DFS

这个题需要保存每层的节点的和以及每层的节点数。采用DFS的方式,把每个节点进行遍历,把这个节点加到对应层中去。每层使用两个数字,第一个数字保存所有节点的和,第二个数字保存有多少个节点。

注意一个小问题,节点为空的时候要return,否则下面node为None,出现错误。

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def averageOfLevels(self, root):
"""
:type root: TreeNode
:rtype: List[float]
"""
info = [] # the first element is sum of the level,the second element is nodes in this level
def dfs(node, depth=0):
if not node:
return
if len(info) <= depth:
info.append([0, 0])
info[depth][0] += node.val
info[depth][1] += 1
# print(info)
dfs(node.left, depth + 1)
dfs(node.right, depth + 1)
dfs(root)
return [s / float(c) for s,c in info]

二刷。直接使用数组保存每层所有节点的值,最后需要做个求平均数的处理。

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def averageOfLevels(self, root):
"""
:type root: TreeNode
:rtype: List[float]
"""
res = []
self.getLevel(root, 0, res)
return [sum(line) / float(len(line)) for line in res] def getLevel(self, root, level, res):
if not root:
return
if level >= len(res):
res.append([])
res[level].append(root.val)
self.getLevel(root.left, level + 1, res)
self.getLevel(root.right, level + 1, res)

方法二:BFS

其实层次遍历使用BFS比使用DFS更加简单高效。因为每层遍历结束之后,已经知道了这一行的所有数字,所以可以直接求平均数,然后放入到结果中去,而不用最后才求平均数了。

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def averageOfLevels(self, root):
"""
:type root: TreeNode
:rtype: List[float]
"""
que = collections.deque()
res = []
que.append(root)
while que:
size = len(que)
row = []
for _ in range(size):
node = que.popleft()
if not node:
continue
row.append(node.val)
que.append(node.left)
que.append(node.right)
if row:
res.append(sum(row) / float(len(row)))
return res

日期

2018 年 1 月 17 日
2018 年 11 月 9 日 —— 睡眠可以

最新文章

  1. 一步步开发自己的博客 .NET版(3、注册登录功能)
  2. atitit.http原理与概论attilax总结
  3. 45分钟带你入门Linux(附:笔者在工作室开讨论班录制的视频讲解)
  4. cxLookupComboBox 控件
  5. ORA-00245: control file backup failed; target is likely on a local file system
  6. 消息队列之RabbitMQ
  7. Java [Leetcode 137]Single Number II
  8. pgsql与mysql 下 varchar类型的数字文本的排序 区别
  9. js中document.write()使用方法
  10. C语言助教批改
  11. string类的简洁版实现
  12. elasticsearch(6) 映射和分析
  13. 吴裕雄 python深度学习与实践(12)
  14. Dapper内部分享ppt
  15. 集成学习算法总结----Boosting和Bagging
  16. Bzoj3352 [ioi2009]旅行商
  17. CentOS每次开机都要ifup eth0的解决方法小结
  18. dede发布内容限制违规词
  19. cocos2d-x 环境搭建
  20. iOS 7 计算字符串高度

热门文章

  1. Rust 指定安装目录
  2. 通用的js异步ajax文件上传函数
  3. 25. Linux下gdb调试
  4. flink-----实时项目---day05-------1. ProcessFunction 2. apply对窗口进行全量聚合 3使用aggregate方法实现增量聚合 4.使用ProcessFunction结合定时器实现排序
  5. 【MarkDown】--使用教程
  6. springboot+vue集成mavon-editor,开发在线文档知识库
  7. Consumer方法结合Lambda表达式的应用
  8. ActiveMQ(二)——ActiveMQ的安装和基本使用
  9. Apache Log4j 2 报高危漏洞,CODING 联手腾讯安全护卫软件安全
  10. hbuilder打包app基本流程