The problem. Given the root of a binary tree, return the average value of the nodes on each level, from the top level down. (Answers within 10⁻⁵ of the true value are accepted.)
Input: root = [3, 9, 20, null, null, 15, 7]
Output: [3.0, 14.5, 11.0]
Input: root = [3, 9, 20, 15, 7]
Output: [3.0, 14.5, 11.0]An average needs two numbers per level: the sum and the count. Any traversal that knows each node's level can collect them — the question is how directly.
Free account
Sign up to read the rest of this lesson: 6 more sections, 2 drawings, a dry-run simulator and code in JavaScript, Python, Java and C++.
Still to come