Given the root of a binary tree, return the average value of the nodes on each level in the form of an array. Answers within 10-5 of the actual answer will be accepted.
&nbsp;
Example 1:

Input: root = [3,9,20,null,null,15,7]
Output: [3.00000,14.50000,11.00000]
Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11.
Hence return [3, 14.5, 11].


Example 2:

Input: root = [3,9,20,15,7]
Output: [3.00000,14.50000,11.00000]


&nbsp;
Constraints:


	The number of nodes in the tree is in the range [1, 104].
	-231 &lt;= Node.val &lt;= 231 - 1


# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
# O(n) || O(h) where h is the height of the tree
    def averageOfLevels(self, root: Optional[TreeNode]) -> List[float]:

        queue = deque([root])
        result = []

        while queue:
            runningSum = 0
            newLevelSize = 0
            for i in range(len(queue)):
                currNode = queue.popleft()

                runningSum += currNode.val
                newLevelSize += 1

                if currNode.left:
                    queue.append(currNode.left)
                if currNode.right:
                    queue.append(currNode.right)

            secretFormula = (runningSum/newLevelSize)

            result.append(secretFormula)

        return result
class Solution {
    public List<Double> averageOfLevels(TreeNode root) {
        Queue<TreeNode> q = new LinkedList<>(List.of(root));
        List<Double> ans = new ArrayList<>();
        while (q.size() > 0) {
            double qlen = q.size(), row = 0;
            for (int i = 0; i < qlen; i++) {
                TreeNode curr = q.poll();
                row += curr.val;
                if (curr.left != null) q.offer(curr.left);
                if (curr.right != null) q.offer(curr.right);
            }
            ans.add(row/qlen);
        }
        return ans;
    }
}
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    // Pre-Requisite for this solution: Level order traversal of a BT Line by Line
    
    vector<double> averageOfLevels(TreeNode* root) {
        // vector to store the result
        vector<double> avgVal;
        avgVal.push_back(root->val);
        
        // Queue for level order traversal Line by Line
        queue<TreeNode*> q;
        q.push(root);
        q.push(NULL);
        
        // To store values for levels
        double levelSum = 0;
        double levelCount = 0;
        
        // Standard Level order traversal Line by Line code
        while(q.empty() == false){
            TreeNode* curr = q.front(); q.pop();
            
            if(curr == NULL){
                if(q.empty()==true){
                    break;
                }
                else{   // Here we know that we are at the end of a line
                    
                    // So we push the result into the vector
                    avgVal.push_back(levelSum/levelCount);
                    
                    // Reset the counters and push End of Level into the queue.
                    levelSum = 0;
                    levelCount = 0;
                    q.push(NULL);
                    continue;
                }
            }
            
            // Note that when we are traversing one level
            // we are getting sum and count of the next level
            // Hence, we are moving one level ahead
            if(curr->left){
                q.push(curr->left);
                levelSum += curr->left->val;
                levelCount++;
            }
            if(curr->right){
                q.push(curr->right);
                levelSum += curr->right->val;
                levelCount++;
            }
        }
        return avgVal;
    }
};
var averageOfLevels = function(root) {
let avg = [root.val];
let level = [];
let queue = [root];
while(queue.length>0){
    let curr = queue.shift();
    if(curr.left){
        level.push(curr.left)
    }
    if(curr.right){
        level.push(curr.right);
    }
    if(queue.length===0){
        queue.push(...level);
        avg.push(level.reduce((a,b) => a+b.val,0)/level.length);
        level = [];
    }
}
avg.pop()
return avg;
};
Average of Levels in Binary Tree