The idea is sum of each level divided by count of each level

```
var averageOfLevels = function(root) {
var sumArr = [];
var countArr = [];
var sum = function(node, level) {
if (!node) return
if (!sumArr[level]) sumArr[level] = 0;
sumArr[level] += node.val;
if (!countArr[level]) countArr[level] = 0;
countArr[level]++;
sum(node.left, level + 1);
sum(node.right, level + 1);
}
sum(root, 0);
var meanArr = sumArr.map((sum, level) => sum / countArr[level]);
return meanArr;
};
```