# Average of Levels in a Binary Tree (Leetcode 637)

Problem Link: <https://leetcode.com/problems/average-of-levels-in-binary-tree/>

```python
class Solution:

    def averageOfLevels(self, root: Optional[TreeNode]) -> List[float]:
        
        s=[root]
        ans=[]
        
        while(len(s)>0):
            summ=0
            num=0
            
            for i in range(len(s)):
                node=s.pop()
                summ+=node.val
                num+=1
                if(node.left is not None):
                    s.insert(0,node.left)

                if(node.right is not None):
                    s.insert(0,node.right)

            ans.append(summ/num)
        return ans
```
