LeetCode-637、二叉树的层平均值-简单
给定一个非空二叉树, 返回一个由每层节点平均值组成的数组.
示例 1:
输入:
3
/ \
9 20
/ \
15 7
输出: [3, 14.5, 11]
解释:
第0层的平均值是 3, 第1层是 14.5, 第2层是 11. 因此返回 [3, 14.5, 11].
注意:节点值的范围在32位有符号整数范围内。
代码:
# 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]:
ans = []
def recursion(nodes):
tmp, count = 0, 0
nex = []
for cur in nodes:
if cur:
tmp += cur.val
count += 1
if cur.left: nex.append(cur.left)
if cur.right: nex.append(cur.right)
tmp /= count
ans.append(tmp)
if nex:
recursion(nex)
recursion([root])
return ans