average_levels_binary_tree
average_levels_binary_tree.py 源码
from typing import List
# 二叉树的层平均值
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
# 广度优先
def averageOfLevels_1(self, root: TreeNode) -> List[float]:
queue, res = [root], []
while len(queue) > 0:
n, count = len(queue), 0
for i in range(n):
node = queue.pop(0)
count += node.val
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
res.append(count / n)
return res
你可能感兴趣的文章
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦