[LintCode] 597. Subtree with Maximum Average

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.
public class Solution {
    private class ResType {
        int sum;
        int size;
        public ResType(int sum, int size) {
            this.sum = sum;
            this.size = size;
        }
    }
    /**
     * @param root: the root of binary tree
     * @return: the root of the maximum average of subtree
     */
    TreeNode maxNode = null;
    ResType globalNode = null;
    public TreeNode findSubtree2(TreeNode root) {
        // write your code here
        helper(root);
        return maxNode;
    }

    private ResType helper(TreeNode root) {
        if (root == null) {
            return new ResType(0, 0);
        }
        ResType left = helper(root.left);
        ResType right = helper(root.right);
        int curSum = left.sum + right.sum + root.val;
        int curSize = left.size + right.size + 1;
        ResType cur = new ResType(curSum, curSize);
        if (maxNode == null || curSum * globalNode.size > curSize * globalNode.sum) {
            globalNode = cur;
            maxNode = root;
        }
        return cur;
    }
}

原文地址:https://www.cnblogs.com/xuanlu/p/12419352.html

时间: 2024-10-15 06:14:16

[LintCode] 597. Subtree with Maximum Average的相关文章

[Leetcode] 1120. Maximum Average Subtree

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.

LeetCode Maximum Average Subarray I

原题链接在这里:https://leetcode.com/problems/maximum-average-subarray-i/description/ 题目: Given an array consisting of n integers, find the contiguous subarray of given length k that has the maximum average value. And you need to output the maximum average v

643. Maximum Average Subarray I 最大子阵列平均数

Given an array consisting of n integers, find the contiguous subarray of given length k that has the maximum average value. And you need to output the maximum average value. Example 1: Input: [1,12,-5,-6,50,3], k = 4Output: 12.75Explanation: Maximum 

643. Maximum Average Subarray I

Given an array consisting of n integers, find the contiguous subarray of given length k that has the maximum average value. And you need to output the maximum average value. Example 1: Input: [1,12,-5,-6,50,3], k = 4 Output: 12.75 Explanation: Maximu

[LeetCode] Maximum Average Subarray I 子数组的最大平均值

Given an array consisting of n integers, find the contiguous subarray of given length k that has the maximum average value. And you need to output the maximum average value. Example 1: Input: [1,12,-5,-6,50,3], k = 4 Output: 12.75 Explanation: Maximu

643. Maximum Average Subarray I 最大子数组的平均值

[抄题]: Given an array consisting of n integers, find the contiguous subarray of given length k that has the maximum average value. And you need to output the maximum average value. Example 1: Input: [1,12,-5,-6,50,3], k = 4 Output: 12.75 Explanation:

lintcode 中等题:maximum subarray difference 最大子数组差

题目 最大子数组差 给定一个整数数组,找出两个不重叠的子数组A和B,使两个子数组和的差的绝对值|SUM(A) - SUM(B)|最大. 返回这个最大的差值. 样例 给出数组[1, 2, -3, 1],返回 6 注意 子数组最少包含一个数 挑战 时间复杂度为O(n),空间复杂度为O(n) 解题 刚做了数组中两个子数组和的最大值,这一题是求差,感觉上题的求解思想应该是可以用的 A B 分别是两个子数组的和,则: 所以 当A>B 的时候A越大越好 B越小越好 当A<B 的时候B越大越好 A越小越好

lintcode 中等题:maximum subarray最大子数组II

题目 最大子数组 II 给定一个整数数组,找出两个不重叠子数组使得它们的和最大. 每个子数组的数字在数组中的位置应该是连续的. 返回最大的和. 您在真实的面试中是否遇到过这个题? Yes 样例 给出数组[1, 3, -1, 2, -1, 2],这两个子数组分别为[1, 3]和[2, -1, 2]或者[1, 3, -1, 2]和[2],它们的最大和都是7 注意 子数组最少包含一个数 挑战 要求时间复杂度为O(n) 解题 最大子数组I 这个题目是求一个数组中一个最大子数组的和,而本题目是求数组中的前

LintCode刷题笔记-- Maximum Product Subarray

动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6. 解题思路: 前面几道题有点儿过分依赖答案了,后面还是需要自己主动