```
def averageOfLevels(self, root):
queue, result = [root], []
while queue:
size = len(queue)
total = 0.0
for _ in range(size):
node = queue.pop()
total += node.val
if node.left:
queue.insert(0, node.left)
if node.right:
queue.insert(0, node.right)
result.append(total / size)
return result
```