Question
Formatted question description: https://leetcode.ca/all/1120.html
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:
The number of nodes in the tree is between 1 and 5000.
Each node will have a value between 0 and 100000.
Answers will be accepted as correct if they are within 10^-5 of the correct answer.
Algorithm
DFS Define a pair<int, int> for each node, the first position represents the sum of the subtree values rooted at that node, and the second position represents the number of nodes of the subtree; Accumulate these two values of each node from the top; The average number of subtrees is the sum/node, which is stored using a global variable; Use dictionaries for memoized searches to speed up
Code
Java
public class Maximum_Average_Subtree {
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
double max = Integer.MIN_VALUE;
public double maximumAverageSubtree(TreeNode root) {
dfs(root);
return max;
}
// double[]: sum, count
private double[] dfs(TreeNode root) {
if (root == null) {
return new double[]{0, 0};
}
double[] l = dfs(root.left);
double[] r = dfs(root.right);
double sum = root.val + l[0] + r[0];
double count = l[1] + r[1] + 1;
double avg = sum / count;
if (avg > max) {
max = avg;
}
return new double[]{sum, count};
}
}
}