LeetCode Solutions/LeetCoding Challenge/March 2020 LeetCoding Challenge/Average of Levels in Binary Tree.java
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Double> averageOfLevels(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>(List.of(root));
        List<Double> averageValue = new ArrayList<>();
        while(queue.size() > 0){
            double queueLength = queue.size(), row = 0;
            for(int i=0; i<queueLength; i++){
                TreeNode current = queue.poll();
                row += current.val;
                if(current.left != null){
                    queue.offer(current.left);
                }
                if(current.right != null){
                    queue.offer(current.right);
                }
            }
            averageValue.add(row/queueLength);
        }
        return averageValue;
    }
}