/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */publicclassSolution { /** * @param root: the binary tree of the root * @return: return a list of double */publicList<Double> averageOfLevels(TreeNode root) {if (root ==null) returnnull;List<Double> res =newArrayList<Double>();Queue<TreeNode> q =newArrayDeque<TreeNode>();q.offer(root);while (!q.isEmpty()) {int size =q.size();double sum =0;for (int i =0; i < size; i++) {TreeNode node =q.poll(); sum +=node.val;if (node.left!=null)q.offer(node.left);if (node.right!=null)q.offer(node.right); }res.add(sum / size); }return res; }}