# LeetCode: 637. 二叉树的层平均值¶

## 1、题目描述¶

输入:
3
/ \
9  20
/  \
15   7



1. 节点值的范围在32位有符号整数范围内。

## 2、解题思路¶

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
from queue import Queue
class Solution:
def averageOfLevels(self, root):
"""
:type root: TreeNode
:rtype: List[float]
"""

if root is None:
return [];

q = Queue()
q.put(root)
result = []
while not q.empty():
nums = q.qsize()
sums = 0
for i in range(nums):
node = q.get()
sums += node.val
if node.left is not None:
q.put(node.left)
if node.right is not None:
q.put(node.right)
result.append(sums / nums)

return result


​ 有看了一个别人的思路，果然python理解的还不够好

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
from queue import Queue
class Solution:
def averageOfLevels(self, root):
"""
:type root: TreeNode
:rtype: List[float]
"""

if root is None:
return [];

row = [root]
result = []
while any(row):
result.append(sum([v.val for v in row]) / len(row))
row = [sub for node in row for sub in (node.left, node.right) if sub]
return result