lintcode597- Subtree with Maximum Average- easy

Posted jasminemzy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lintcode597- Subtree with Maximum Average- easy相关的知识,希望对你有一定的参考价值。

Given a binary tree, find the subtree with maximum average. Return the root of the subtree.

 Notice

LintCode will print the subtree which root is your return node.
It‘s guaranteed that there is only one subtree with maximum average.

Example

Given a binary tree:

     1
   /    -5     11
 / \   /  1   2 4    -2 

return the node 11.

 

用分治+遍历(和全局变量每次打擂台)来做。思路就是分治法每次的的确确返回了当前的值供上一层用,但每次计算的过程中顺便打个擂台,如果本次赢了就存到全局变量里。最后只要返回全局变量即可。

1.注意避免13/4 > 3 是正确的却被乌龙掉的情况。可以1.average算的时候要用double,或者2.把两个int的除法转化为乘法!!!a/b < c/d转为 a*d < b*c,但要分母是正的的情况,本题average的分母正好是count,没问题。

2.需要返回多个数据的时候(比如本题sum和count),helper可以array或者list来存这些数据返回。或者你可以自定义一个写好constructorResultType的类,这个更好,甚至能解决要返回不同类型数据的情况。

3.记得Double的最负值是Double.NEGATIVE_INFINITY,不是MIN_VALUE,MIN_VALUE是正数的最接近0的数。还有NEGATIVE中间有个A别拼错了。

 

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */


public class Solution {
    /*
     * @param root: the root of binary tree
     * @return: the root of the maximum average of subtree
     */
     
     private TreeNode subtree;
     private double average = Double.NEGATIVE_INFINITY;
     
    public TreeNode findSubtree2(TreeNode root) {
        // write your code here
        if (root == null) {
            return null;
        }
        
        helper(root);
        return subtree;
        
    }
    
    private List<Integer> helper(TreeNode root) {
        
        List<Integer> result = new ArrayList<Integer>();
        
        if (root == null) {
            result.add(0);
            result.add(0);
            return result;
        }
        
        List<Integer> leftResult = helper(root.left);
        List<Integer> rightResult = helper(root.right);
        
        int crtCount = leftResult.get(0) + rightResult.get(0) + 1;
        int crtSum = leftResult.get(1) + rightResult.get(1) + root.val;
        double crtAve = (double)crtSum / crtCount;
        
        if (crtAve > average) {
            average = crtAve;
            subtree = root;
        }
        
        result.add(crtCount);
        result.add(crtSum);
        return result;
        
    }
}

 

九章的实现:

/**
* 本参考程序来自九章算法,由 @九章算法 提供。版权所有,转发请注明出处。
* - 九章算法致力于帮助更多中国人找到好的工作,教师团队均来自硅谷和国内的一线大公司在职工程师。
* - 现有的面试培训课程包括:九章算法班,系统设计班,算法强化班,Java入门与基础算法班,android 项目实战班,
* - Big Data 项目实战班,算法面试高频题班, 动态规划专题班
* - 更多详情请见官方网站:http://www.jiuzhang.com/?source=code
*/ 

// version 1: Traverse + Divide Conquer
public class Solution {
    private class ResultType {
        public int sum, size;
        public ResultType(int sum, int size) {
            this.sum = sum;
            this.size = size;
        }
    }
    
    private TreeNode subtree = null;
    private ResultType subtreeResult = null;
    
    /**
     * @param root the root of binary tree
     * @return the root of the maximum average of subtree
     */
    public TreeNode findSubtree2(TreeNode root) {
        helper(root);
        return subtree;
    }
    
    private ResultType helper(TreeNode root) {
        if (root == null) {
            return new ResultType(0, 0);
        }
        
        ResultType left = helper(root.left);
        ResultType right = helper(root.right);
        ResultType result = new ResultType(
            left.sum + right.sum + root.val,
            left.size + right.size + 1
        );
        
        if (subtree == null ||
            subtreeResult.sum * result.size < result.sum * subtreeResult.size
        ) {
            subtree = root;
            subtreeResult = result;
        }
        return result;
    }
}

 

以上是关于lintcode597- Subtree with Maximum Average- easy的主要内容,如果未能解决你的问题,请参考以下文章

LintCode Subtree

lintcode-easy-Subtree

lintcode596- Minimum Subtree- easy

LeetCode 865. Smallest Subtree with all the Deepest Nodes

866. Smallest Subtree with all the Deepest Nodes

865. Smallest Subtree with all the Deepest Nodes