Given a non-empty binary tree, print the average value of the nodes on each level.
Input : 4 / \ 2 9 / \ \ 3 5 7 Output : [4 5.5 5] The average value of nodes on level 0 is 4, on level 1 is 5.5, and on level 2 is 5. Hence, print [4 5.5 5].
The idea is based on Level order traversal line by line | Set 2 (Using Two Queues)
- Start by pushing the root node into the queue. Then, remove a node from the front of the queue.
- For every node removed from the queue, push all its children into a new temporary queue.
- Keep on popping nodes from the queue and adding these node’ s children to the temporary queue till queue becomes empty.
- Every time queue becomes empty, it indicates that one level of the tree has been considered.
- While pushing the nodes into temporary queue, keep a track of the sum of the nodes along with the number of nodes pushed and find out the average of the nodes on each level by making use of these sum and count values.
- After each level has been considered, again initialize the queue with temporary queue and continue the process till both queues become empty.
Average of levels: [4 5.5 5]
Time complexity : O(n).
The whole tree is traversed atmost once. Here, n refers to the number of nodes in the given binary tree.
- Auxiliary Space : O(n).
The size of queues can grow upto atmost the maximum number of nodes at any level in the given binary tree. Here, n refers to the maximum number of nodes at any level in the input tree.
This article is contributed by Aakash Pal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Print all K-sum levels in a Binary Tree
- Print all Prime Levels of a Binary Tree
- Left-Right traversal of all the levels of Binary tree
- Print all Palindromic Levels Of a Binary Tree
- Print all nodes between two given levels in Binary Tree
- Print Levels of all nodes in a Binary Tree
- Print all Co-Prime Levels of a Binary Tree
- Print all Exponential Levels of a Binary Tree
- Reverse alternate levels of a perfect binary tree
- Print Binary Tree levels in sorted order | Set 2 (Using set)
- Maximum sum of leaf nodes among all levels of the given binary tree
- Maximum sum of non-leaf nodes among all levels of the given binary tree
- Print Binary Tree levels in sorted order
- Number of edges in a perfect binary tree with N levels
- Reverse alternate levels of a perfect binary tree using Stack
- Print even positioned nodes of even levels in level order of the given binary tree
- Print odd positioned nodes of even levels in level order of the given binary tree
- Print odd positioned nodes of odd levels in level order of the given binary tree
- Print even positioned nodes of odd levels in level order of the given binary tree