Given the root of a binary tree, return the average value of the nodes on each level in the form of an array. Answers within 10-5 of the actual answer will be accepted.
root = [3,9,20,null,null,15,7][3.0, 14.5, 11.0]root = [1,2,3,4,5][1.0, 2.5, 4.5]root = [1][1.0]sz = queue.size() — node count at current level; init sum = 0sz nodes: sum += node.val, enqueue non-null childrenavg = sum / sz, add to result; loop until queue emptyExtending level order BFS — instead of collecting values, we accumulate a running sum. By capturing sz = queue.size() before the inner loop, we guarantee sum contains exactly the current level's nodes. avg = sum / sz gives the level average. Without the snapshot, children added during iteration would corrupt the level boundary and inflate the sum.