LintCode 597: Subtree with MaximumAverage (Binary Tree经典题)

  1. 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:
代码如下:
注意:

  1. int的除法要注意除数不为0, 并且结果要转换为double!)
  2. 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;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值