':type root: TreeNode
:rtype: List[float]'

def averageOfLevels(self, root):

    result = []
    q = collections.deque([root])
    while q:
        (total, count) = (0, 0)
        next_q = collections.deque([])
        while q:
            n = q.popleft()
            total += n.val
            count += 1
            if n.left:
                next_q.append(n.left)
            if n.right:
                next_q.append(n.right)
        (q, next_q) = (next_q, q)
        result.append((float(total) / count))
    return result
