Subtree with Maximum Average

https://leetcode.com/discuss/interview-question/349617#:~:text=Amazon%20%7C%20OA%202019%20%7C%20Subtree%20with%20Maximum%20Average,-88&text=A%20subtree%20of%20a%20tree,by%20the%20number%20of%20nodes.

Given an N-ary tree, find the subtree with the maximum average. Return the root of the subtree. A subtree of a tree is the node which have at least 1 child plus all its descendants. The average value of a subtree is the sum of its values, divided by the number of nodes.

Example 1:

Input:
            20
	   /   \
	 12     18
      /  |  \   / \
    11   2   3 15  8

Output: 18
Explanation:
There are 3 nodes which have children in this tree:
12 => (11 + 2 + 3 + 12) / 4 = 7
18 => (18 + 15 + 8) / 3 = 13.67
20 => (12 + 11 + 2 + 3 + 18 + 15 + 8 + 20) / 8 = 11.125

18 has the maximum average so output 18.

Similar questions:

Last updated