![]() |
VOOZH | about |
Given a Binary Tree consisting of N nodes, the task to find the maximum average of values of nodes of any subtree.
Examples:
Input: 5
/ \
3 8
Output: 8
Explanation:
Average of values of subtree of node 5 = (5 + 3 + 8) / 3 = 5.33
Average of values of subtree of node 3 = 3 / 1 = 3
Average of values of subtree of node 8 = 8 / 1 = 8.Input: 20
/ \
12 18
/ \ / \
11 3 15 8
Output: 15
Explanation: Average of values of subtree of node 15 is the maximum.
Approach: The given problem is similar to that of finding the largest subtree sum in a tree. The idea is to use DFS traversal technique. Follow the steps below to solve the problem:
15.0
Time Complexity: O(N)
Auxiliary Space: O(N)