hardTreesPure DSA~30 min
Subtree With the Maximum Average Value
Across all subtrees of a binary tree, find the largest average node value (a single node counts as its own subtree).
Problem
Given the root of a binary tree, return the maximum average value over all subtrees. A subtree consists of a node and all of its descendants; the average is the sum of its values divided by its node count.
Input
A binary tree root with integer node values.
Output
A floating-point number: the maximum subtree average.
Constraints
- 1 <= number of nodes <= 10^4
- 0 <= node value <= 10^5
- Answers within 1e-5 are accepted.
Examples
Example 1
Input
root = [5,6,1]
Output
6.00000
The single node 6 has the highest average.
Example 2
Input
root = [0,null,1]
Output
1.00000
The subtree rooted at 1 averages 1.