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:
Algorithm
\nOne 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:
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.
\nCall 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.
Repeat the above steps till all the nodes in the given tree have been considered once.
\nPopulate the averages in the resultant array to be returned.
\nThe following animation illustrates the process.
\n!?!../Documents/637_Avg_of_Levels_DFS.json:1000,563!?!
\n\nComplexity Analysis
\nTime complexity : . The whole tree is traversed once only. Here, refers to the total number of nodes in the given binary tree.
\nSpace 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.
\nAlgorithm
\nAnother 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:
\nPut the root node into the .
\nInitialize and as 0 and as an empty queue.
\nPop 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.
\nPut the children nodes of the node last popped into the a queue(instead of ).
\nContinue steps 3 and 4 till becomes empty. (An empty indicates that one level of the tree has been considered).
\nReinitialize with its value as .
\nPopulate the array with the average corresponding to the current level.
\nRepeat steps 2 to 7 till the and become empty.
\nAt the end, is the required result.
\nThe following animation illustrates the process.
\n!?!../Documents/637_Average_Of_Levels.json:1000,563!?!
\n\nComplexity Analysis
\nTime complexity : . The whole tree is traversed atmost once. Here, refers to the number of nodes in the given binary tree.
\nSpace 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.
\nAnalysis written by: @vinod23
\n