-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path637.py
More file actions
25 lines (23 loc) · 768 Bytes
/
637.py
File metadata and controls
25 lines (23 loc) · 768 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Runtime: 92 ms, faster than 22.01% of Python3 online submissions for Average of Levels in Binary Tree.
# Difficulty: Easy
class Solution:
def averageOfLevels(self, root):
"""
:type root: TreeNode
:rtype: List[float]
"""
values = list()
def helper(tree, level):
if tree == None:
return
if len(values) <= level:
values.append([tree.val])
else:
values[level].append(tree.val)
helper(tree.left, level + 1)
helper(tree.right, level + 1)
helper(root, 0)
averages = list()
for value in values:
averages.append(sum(value) / len(value))
return averages