Average of Levels in Binary Tree
EasyTreeslevel order
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
Given the root of a non-empty binary tree, compute the average node value on each level and return the averages as a list, from the root level downward.
Averages are real numbers and are not rounded; answers within 1e-6 of the exact value are accepted.
Examples
Input: root = [3,9,20,null,null,15,7]
Output: [3.0,14.5,11.0]
Level 0: 3. Level 1: (9+20)/2 = 14.5. Level 2: (15+7)/2 = 11.
Input: root = [5,-3,8,1]
Output: [5.0,2.5,1.0]
Level 1 averages -3 and 8; level 2 holds only 1.
Constraints
- 1 <= number of nodes <= 10^4
- -10^4 <= Node.val <= 10^4
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.