Subtree with Maximum Average
Given a M-ary tree, find the subtree with maximum average. Return the root of the subtree.
A subtree of a tree is any node of that tree plus all its descendants. The average value of a subtree is the sum of its values, divided by the number of nodes.
Explanation: For the node with value = 13 we have an average of (13 + 4 + -2) / 3 = 5 which is the maximum.
For the node with value = 1 we have an average of (- 5 + 21 + 5 - 1) / 5 = 4.
For the node with value = -5 we have an average of (-5 / 1) = -5.
For the node with value = 21 we have an average of (21 / 1) = 21.
For the node with value = 5 we have an average of (5 / 1) = 5.
For the node with value = -1 we have an average of (-1 / 1) = -1.
So the subtree of 21 is the maximum.
(tips: do you know in Java, the value of Double.MIN_VALUE is actually positive? )
Solve the problem:Python3
Get one-to-one training from Google Facebook engineers
Learn from Facebook and Google senior engineers interviewed 100+ candidates.
Most recent interview questions and system design topics gathered from aonecode alumnus.
One-to-one online classes. Get feedbacks from real interviewers.
Customized Private Class
Already a coding expert? - Advance straight to hard interview topics of your interest.
New to the ground? - Develop basic coding skills with your own designated mentor.
Days before interview? - Focus on most important problems in target company question bank.