- Subtree with Maximum Average
Description
Given a binary tree, find the subtree with maximum average. Return the root of the subtree.
Example 1
Input:
{1,-5,11,1,2,4,-2}
Output:11
Explanation:
The tree is look like this:
1
/ \
-5 11
/ \ / \
1 2 4 -2
The average of subtree of 11 is 4.3333, is the maximun.
Example 2
Input:
{1,-5,11}
Output:11
Explanation:
1
/ \
-5 11
The average of subtree of 1,-5,11 is 2.333,-5,11. So the subtree of 11 is the maximun.
Notice
LintCode will print the subtree which root is your return node.
It’s guaranteed that there is only one subtree with maximum average.
思路: postread 遍历 + 递归。
这种题目都必须自定义ResultType。
解法1:
代码如下:
注意:
- int的除法要注意除数不为0, 并且结果要转换为double!)
- maxResult的初始化用ResultType(NULL, INT_MIN, 0) 或 ResultType(NULL, 0, 0) 都可以。因为最后是用count==0来判断。
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
#include <limits>
struct ResultType {
TreeNode * node;
int sum;
int count;
ResultType(TreeNode * t = NULL, int s = 0, int c = 0) : node(t), sum(s), count(c) {}
};
class Solution {
public:
/**
* @param root: the root of binary tree
* @return: the root of the maximum average of subtree
*/
TreeNode * findSubtree2(TreeNode * root) {
if (!root) return NULL;
maxResult = ResultType(NULL, 0, 0);
helper(root);
return maxResult.node;
}
ResultType helper(TreeNode * root) {
if (!root) return ResultType();
ResultType left = helper(root->left);
ResultType right = helper(root->right);
int sum = left.sum + right.sum + root->val;
int count = left.count + right.count + 1;
ResultType newResult = ResultType(root, sum, count);
if ((maxResult.count == 0) ||
((double)maxResult.sum / maxResult.count < (double)newResult.sum / newResult.count)) {
maxResult = newResult;
}
return newResult;
}
private:
ResultType maxResult;
};
解法2:
参考网上的。思路差不多,但将除法变成乘法,去掉很多麻烦,还提高效率。很妙。
代码如下:
class Solution {
public:
/**
* @param root: the root of binary tree
* @return: the root of the maximum average of subtree
*/
TreeNode * findSubtree2(TreeNode * root) {
if (!root) return NULL;
maxResult = ResultType(NULL, INT_MIN, 0);
helper(root);
return maxResult.node;
}
ResultType helper(TreeNode * root) {
if (!root) return ResultType();
ResultType left = helper(root->left);
ResultType right = helper(root->right);
int sum = left.sum + right.sum + root->val;
int count = left.count + right.count + 1;
ResultType newResult = ResultType(root, sum, count);
//division -> multiply, reducing time!!!
if (sum * maxResult.count > maxResult.sum * count) {
maxResult = newResult;
}
return newResult;
}
private:
ResultType maxResult;
};
三刷:
class Solution {
public:
/**
* @param root: the root of binary tree
* @return: the root of the maximum average of subtree
*/
TreeNode * findSubtree2(TreeNode * root) {
int cnt = 0;
getSum(root, cnt);
return maxAvgNode;
}
private:
double maxAvg = LLONG_MIN;
TreeNode *maxAvgNode = NULL;
double getSum(TreeNode *root, int &cnt) {
if (!root) {
cnt = 0;
return 0;
}
int cnt_left, cnt_right;
double res = getSum(root->left, cnt_left) + getSum(root->right, cnt_right) + root->val;
cnt = cnt_left + cnt_right + 1;
if (maxAvg < res / cnt) {
maxAvg = res / cnt;
maxAvgNode = root;
}
return res;
}
};