/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Double> averageOfLevels(TreeNode root) {
List<Double> rl = new LinkedList<>();
List<TreeNode> tre = new LinkedList<>();
tre.add(root);
while(!tre.isEmpty()){
int size = tre.size();
double sum = 0;
for(int i = 0;i < size;i++){
TreeNode n = tre.remove(0);
sum = sum + n.val;
if(n.left != null)
tre.add(n.left);
if(n.right != null)
tre.add(n.right);
}
rl.add(sum / size);
}
return rl;
}
}
leetcode——637
最新推荐文章于 2023-12-25 17:55:07 发布