637. Average of Levels in Binary Tree

Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array.

Example 1:

```Input:
3
/ \
9  20
/  \
15   7
Output: [3, 14.5, 11]
Explanation:
The average value of nodes on level 0 is 3,  on level 1 is 14.5, and on level 2 is 11. Hence return [3, 14.5, 11].
```

Note:

1. The range of node's value is in the range of 32-bit signed integer.

b'
\n\n

Solution

\n
\n

Approach #1 Using Depth First Search [Accepted]

\n

Algorithm

\n

One of the methods to solve the given problem is to make use of Depth First Search. In DFS, we try to exhaust each branch of the given tree during the tree traversal before moving onto the next branch.

\n

To make use of DFS to solve the given problem, we make use of two lists and . Here, refers to the total number of nodes found at the level(counting from root at level 0) till now, and refers to the sum of the nodes at the level encountered till now during the Depth First Search.

\n

We make use of a function `average(t, i, res, count)`, which is used to fill the and array if we start the DFS from the node at the level in the given tree. We start by making the function call `average(root, 0, res, count)`. After this, we do the following at every step:

\n
\n
1. \n

Add the value of the current node to the (or ) at the index corresponding to the current level. Also, increment the at the index corresponding to the current level.

\n
2. \n
3. \n

Call the same function, `average`, with the left and the right child of the current node. Also, update the current level used in making the function call.

\n
4. \n
5. \n

Repeat the above steps till all the nodes in the given tree have been considered once.

\n
6. \n
7. \n

Populate the averages in the resultant array to be returned.

\n
8. \n
\n

The following animation illustrates the process.

\n

!?!../Documents/637_Avg_of_Levels_DFS.json:1000,563!?!

\n\n

Complexity Analysis

\n
\n
• \n

Time complexity : . The whole tree is traversed once only. Here, refers to the total number of nodes in the given binary tree.

\n
• \n
• \n

Space complexity : . and array of size are used. Here, refers to the height(maximum number of levels) of the given binary tree. Further, the depth of the recursive tree could go upto only.

\n
• \n
\n
\n

Approach #2 Breadth First Search [Accepted]

\n

Algorithm

\n

Another method to solve the given problem is to make use of a Breadth First Search. In BFS, we start by pushing the root node into a . Then, we remove an element(node) from the front of the . For every node removed from the , we add all its children to the back of the same . We keep on continuing this process till the becomes empty. In this way, we can traverse the given tree on a level-by-level basis.

\n

But, in the current implementation, we need to do a slight modification, since we need to separate the nodes on one level from that of the other.

\n

The steps to be performed are listed below:

\n
\n
1. \n

Put the root node into the .

\n
2. \n
3. \n

Initialize and as 0 and as an empty queue.

\n
4. \n
5. \n

Pop a node from the front of the . Add this node\'s value to the corresponding to the current level. Also, update the corresponding to the current level.

\n
6. \n
7. \n

Put the children nodes of the node last popped into the a queue(instead of ).

\n
8. \n
9. \n

Continue steps 3 and 4 till becomes empty. (An empty indicates that one level of the tree has been considered).

\n
10. \n
11. \n

Reinitialize with its value as .

\n
12. \n
13. \n

Populate the array with the average corresponding to the current level.

\n
14. \n
15. \n

Repeat steps 2 to 7 till the and become empty.

\n
16. \n
\n

At the end, is the required result.

\n

The following animation illustrates the process.

\n

!?!../Documents/637_Average_Of_Levels.json:1000,563!?!

\n\n

Complexity Analysis

\n
\n
• \n

Time complexity : . The whole tree is traversed atmost once. Here, refers to the number of nodes in the given binary tree.

\n
• \n
• \n

Space complexity : . The size of or can grow upto atmost the maximum number of nodes at any level in the given binary tree. Here, refers to the maximum mumber of nodes at any level in the input tree.

\n
• \n
\n
\n

Analysis written by: @vinod23

\n
'