Skip to content

Latest commit

 

History

History

maximum-average-subtree

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

< Previous                  Next >

Given the root of a binary tree, find the maximum average value of any subtree of that tree.

(A subtree of a tree is any node of that tree plus all its descendants. The average value of a tree is the sum of its values, divided by the number of nodes.)

 

Example 1:

Input: [5,6,1]
Output: 6.00000
Explanation: 
For the node with value = 5 we have an average of (5 + 6 + 1) / 3 = 4.
For the node with value = 6 we have an average of 6 / 1 = 6.
For the node with value = 1 we have an average of 1 / 1 = 1.
So the answer is 6 which is the maximum.

 

Note:

  1. The number of nodes in the tree is between 1 and 5000.
  2. Each node will have a value between 0 and 100000.
  3. Answers will be accepted as correct if they are within 10^-5 of the correct answer.

Related Topics

[Tree] [Depth-First Search] [Binary Tree]

Similar Questions

  1. Count Nodes Equal to Sum of Descendants (Medium)

Hints

Hint 1 Can you find the sum of values and the number of nodes for every sub-tree ?
Hint 2 Can you find the sum of values and the number of nodes for a sub-tree given the sum of values and the number of nodes of it's left and right sub-trees ?
Hint 3 Use depth first search to recursively find the solution for the children of a node then use their solutions to compute the current node's solution.