You are given the root of a binary tree. Each node in the tree has an integer value. Your task is to find the subtree with the maximum average value.
A subtree of a node x
includes x
and all of x
's descendants.
The average value of a subtree is the sum of all node values in the subtree divided by the number of nodes in the subtree.
For example, consider the following binary tree:
2
/ \
1 3
The subtree rooted at node 2
has a sum of 2 + 1 + 3 = 6 and a count of 3. The average is 6/3 = 2.
The subtree rooted at node 1
has a sum of 1 and a count of 1. The average is 1/1 = 1.
The subtree rooted at node 3
has a sum of 3 and a count of 1. The average is 3/1 = 3.
In this case, the subtree rooted at node 3
has the maximum average, which is 3.
Write a function that takes the root of a binary tree as input and returns the maximum average value among all subtrees.
What is the time and space complexity of your solution? Are there any edge cases to consider, such as an empty tree?
The brute-force approach involves traversing every node in the tree and considering it as the root of a subtree. For each node, we calculate the sum of all nodes in its subtree and the number of nodes in that subtree. The average is then computed. We keep track of the maximum average found so far and return it.
This approach explores all possible subtrees, which is computationally expensive.
A more efficient solution involves using a post-order traversal of the tree. During the post-order traversal, we calculate the sum and count of nodes in each subtree. We then calculate the average of each subtree and update the maximum average found so far. By doing this in a single traversal, we avoid redundant calculations.
helper(node)
that returns a tuple (sum, count)
for the subtree rooted at node
.helper
function:
node
is null
, return (0, 0)
. (Edge Case)helper
on the left and right children to get their sums and counts.(sum, count)
tuple for the current subtree.helper
function on the root of the tree./**
* 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 {
private double maxAvg = Double.MIN_VALUE;
public double maximumAverageSubtree(TreeNode root) {
if (root == null) {
return 0.0;
}
helper(root);
return maxAvg;
}
private int[] helper(TreeNode node) {
if (node == null) {
return new int[] {0, 0};
}
int[] left = helper(node.left);
int[] right = helper(node.right);
int sum = left[0] + right[0] + node.val;
int count = left[1] + right[1] + 1;
double avg = (double) sum / count;
maxAvg = Math.max(maxAvg, avg);
return new int[] {sum, count};
}
}