Average of Levels in Binary Tree
Average of Levels in Binary Tree
Given the root of a non-empty binary tree, return the average value of the nodes on each level in the form of an array.
Examples
Example 1:
Input: root = [3, 9, 20, null, null, 15, 7]
Output: [3.0, 14.5, 11.0]
3
/ \
9 20
/ \
15 7Example 2:
Input: root = [3, 9, 20, 15, 7]
Output: [3.0, 14.5, 11.0]Constraints
- The number of nodes in the tree is in the range
[1, 10^4]. -2^31 <= Node.val <= 2^31 - 1
Expected Complexity
- Time: O(n)
- Space: O(n)
MEDIUM
Binary Tree
BFS
Queue
Level Order
Intermediate
0 views
Solution
Hints
Hint 1
Hint 2
Premium
Hint 3
Premium
Hint 4
Premium
This section is available for CodeSnatch Premium members only.
