```
class Solution(object):
def averageOfLevels(self, root):
ans = []
lvl = [root]
while lvl:
ans.append(sum(n.val for n in lvl) / float(len(lvl)))
lvl = [c for n in lvl for c in [n.left, n.right] if c]
return ans
```