Generation Averages
A lab grows a cell culture from one starting cell, and each cell splits into at most two daughters. The family is a binary tree whose nodes hold cell sizes in micrometres. To see whether cells shrink over time, the lab wants the average size of each generation: the starting cell, its daughters, theirs, and so on.
Your task
Write generationAverages(root). Return a list with the average node value of each level of the tree, from the top level down.
An empty tree gives an empty list. Trees are shown in level order, like [12, 8, 10, 7, 9, null, 15], where null marks a missing child. Your function receives real TreeNode objects with val, left and right.
generationAverages(root: TreeNode) → float[] Numbers are accepted within 1e-6.
Examples
root = [12, 8, 10, 7, 9, null, 15]
[12, 9, 10.333333333333334]
Generation 1 is the single cell 12. Generation 2 is 8 and 10, which average 9. Generation 3 is 7, 9 and 15 (null is a missing cell), which average 31 / 3 = 10.333…
root = [5]
[5]
The one cell never split, so there is a single generation with average 5.
Constraints
- 0 ≤ number of cells ≤ 10,000
- -1,000,000 ≤ size ≤ 1,000,000
- Answers within 0.000001 of the expected value are accepted.
Start with brute force
A depth-first walk that passes the depth along and keeps a sum and a count for each depth is also O(n) and works well. Breadth-first search matches the question more directly, because it gives you one level at a time.
Hints
0 of 4 shown- Hint 1
Ignore the averages for now. How would you collect the cells of generation 2, and then those of generation 3?
- Hint 2
Each generation is exactly the children of the previous one. From a list of generation k, you can build generation k + 1.
- Hint 3
Process the tree a level at a time with a queue. How do you know where one level ends? What is the queue's length right when a level starts?
- Hint 4
Put the root in a queue. While it is not empty: read
size = queue.length, pop exactlysizenodes, sum their values, push their children, then recordsum / size.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Breadth-first search visits the tree level by level. The only extra step is handling one whole level at a time so you can average it.
Trace [12, 8, 10, 7, 9, null, 15]:
- The first level is [12], with sum 12 and size 1, so the average is 12. Its children are [8, 10].
- The second level is [8, 10], with sum 18 and size 2, so the average is 9. Its children are [7, 9, 15].
- The third level is [7, 9, 15], with sum 31 and size 3, so the average is 10.333…. These nodes have no children, so the loop stops.
The JavaScript version builds a fresh array for each level, while the Python version reads the queue's length at the start of each level. Both do the same thing.
function generationAverages(root) {
const result = [];
if (!root) return result;
let queue = [root];
while (queue.length > 0) {
let sum = 0;
const next = [];
for (const node of queue) {
sum += node.val;
if (node.left) next.push(node.left);
if (node.right) next.push(node.right);
}
result.push(sum / queue.length);
queue = next;
}
return result;
}from collections import deque
def generation_averages(root):
result = []
if root is None:
return result
queue = deque([root])
while queue:
size = len(queue)
total = 0
for _ in range(size):
node = queue.popleft()
total += node.val
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
result.append(total / size)
return resultRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.