text: # Definition for a binary tree node.
  # class TreeNode:
  #     def __init__(self, x):
  #         self.val = x
  #         self.left = None
  #         self.right = None
  
  class Solution:
      def averageOfLevels(self, root):
          """
          :type root: TreeNode
          :rtype: List[float]
          """
          if not root:
              return []
          queue = [root]
          ans = []
          while queue:
              l = len(queue)
              s = 0
              for _ in range(len(queue)):
                  temp = queue.pop(0)
                  s += temp.val
                  if temp.left:
                      queue.append(temp.left)
                  if temp.right:
                      queue.append(temp.right)
              
              ans.append(s / l)
          return ans