-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
27 lines (26 loc) · 743 Bytes
/
solution.py
File metadata and controls
27 lines (26 loc) · 743 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
26
27
# 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]
"""
if not root:
return []
res = []
level = [root]
while len(level) > 0:
res.append(sum([x.val for x in level]) / (len(level) * 1.0))
temp = []
for node in level:
if node.left:
temp.append(node.left)
if node.right:
temp.append(node.right)
level = temp
return res