Search⌘ K
AI Features

Solution: Average of Levels in Binary Tree

Explore how to compute the average values of nodes at each level in a binary tree by implementing a breadth-first search traversal. This lesson guides you to use a queue to process nodes level by level, track sums and counts, and efficiently calculate averages, preparing you to solve similar tree traversal problems.

Statement

You are given the root of a binary tree. Your task is to return the average value of the nodes on each level in the form of an array.

Note: Only answers within 10510^{-5} of the actual answer will be accepted.

Constraints:

  • The number of nodes in the tree is in the range [1,104][1, 10^4] ...