# 0637. Average of Levels in Binary Tree ###### tags: `Leetcode` `BFS` `Easy` `Level Order Traversal` Link: https://leetcode.com/problems/average-of-levels-in-binary-tree/ ## 思路 层序遍历 注意overflow ## Code ```java= class Solution { public List<Double> averageOfLevels(TreeNode root) { Queue<TreeNode> q = new LinkedList<>(); q.add(root); List<Double> ans = new ArrayList<>(); while(!q.isEmpty()){ long sum = 0; long size = q.size(); for(int i=0; i<size; i++){ TreeNode curr = q.poll(); if(curr.left!=null) q.add(curr.left); if(curr.right!=null) q.add(curr.right); sum += curr.val; } ans.add(sum*1.0/size); } return ans; } } ```