This website contains ALL LeetCode **Premium** problems for
**FREE!!**.

All leaked interview problems are collected from Internet.

All leaked interview problems are collected from Internet.

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 7Output:[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:**

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

b'

\n## Solution

\n

\n#### Approach #1 Using Depth First Search [Accepted]

\n

\n#### Approach #2 Breadth First Search [Accepted]

\n

\n

'
\n\n

\n\n

**Algorithm**

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.

\nTo 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.

\nWe 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
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 \n - \n
Call the same function,

\n`average`

, with the left and the right child of the current node. Also, update the current level used in making the function call. \n - \n
Repeat the above steps till all the nodes in the given tree have been considered once.

\n \n - \n
Populate the averages in the resultant array to be returned.

\n \n

The following animation illustrates the process.

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

\n\n**Complexity Analysis**

- \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

**Algorithm**

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.

\nBut, 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.

\nThe steps to be performed are listed below:

\n- \n
- \n
Put the root node into the .

\n \n - \n
Initialize and as 0 and as an empty queue.

\n \n - \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 \n - \n
Put the children nodes of the node last popped into the a queue(instead of ).

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

\n \n - \n
Reinitialize with its value as .

\n \n - \n
Populate the array with the average corresponding to the current level.

\n \n - \n
Repeat steps 2 to 7 till the and become empty.

\n \n

At the end, is the required result.

\nThe following animation illustrates the process.

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

\n\n**Complexity Analysis**

- \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

Analysis written by: @vinod23

\n