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.)

Example 1:

Input: [5,6,1]
Output: 6.00000
Explanation:
For the node with value = 5 we have and average of (5 + 6 + 1) / 3 = 4.
For the node with value = 6 we have and average of 6 / 1 = 6.
For the node with value = 1 we have and average of 1 / 1 = 1.
So the answer is 6 which is the maximum.

Note:

  1. The number of nodes in the tree is between 1 and 5000.
  2. Each node will have a value between 0 and 100000.
  3. Answers will be accepted as correct if they are within 10^-5 of the correct answer.

===================================================================

C#

 using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
List<double> avgs = new List<double>();
public double MaximumAverageSubtree(TreeNode root)
{
int counter = ;
double summer = ;
var result = TreeTraverse(root,ref counter,ref summer); return avgs.Max();
} public double? TreeTraverse(TreeNode subTreeRoot,ref int counter,ref double summer)
{
if(subTreeRoot == null)
{
return null;
}
int leftCounter = ;
int rightCounter = ;
double leftSummer = ;
double rightSummer = ; double? leftSum = TreeTraverse(subTreeRoot.left,ref leftCounter,ref leftSummer); if (false == leftSum.HasValue)
{
leftSum = ;
}
else
counter++; double? rightSum = TreeTraverse(subTreeRoot.right,ref rightCounter,ref rightSummer); if (false == rightSum.HasValue)
{
rightSum = ;
}
else
counter++; counter = (leftCounter + rightCounter + );
summer = (leftSummer + rightSummer + subTreeRoot.val);
avgs.Add(summer / counter); return subTreeRoot.val;
}
}

[Leetcode] 1120. Maximum Average Subtree的更多相关文章

  1. 【LeetCode】1120. Maximum Average Subtree 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...

  2. LeetCode 643. Maximum Average Subarray I (最大平均值子数组之一)

    Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...

  3. [LeetCode] 644. Maximum Average Subarray II 子数组的最大平均值之二

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

  4. [Leetcode]643. Maximum Average Subarray I

    Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...

  5. [LeetCode] 643. Maximum Average Subarray I_Easy tag: Dynamic Programming(Sliding windows)

    Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...

  6. leetcode 643. Maximum Average Subarray I 子数组最大平均数 I

    一.题目大意 https://leetcode.cn/problems/maximum-average-subarray-i/ 给你一个由 n 个元素组成的整数数组 nums 和一个整数 k . 请你 ...

  7. [LeetCode] Maximum Average Subarray II 子数组的最大平均值之二

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

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

    Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...

  9. LeetCode Maximum Average Subarray I

    原题链接在这里:https://leetcode.com/problems/maximum-average-subarray-i/description/ 题目: Given an array con ...

随机推荐

  1. Python从零开始——安装与运行

  2. 豆瓣读书isbn 查询

    最近学习微信小程序,做一个类似"书库"的小demo,大致流程使用摄像头获取书本后面的isbn,通过豆瓣读书API得到书本介绍.豆瓣评分.图书评论等信息,然鹅https://api. ...

  3. 在 Visual Studio 中安装 FxCop 分析器

    本文转自 微软官网 : https://docs.microsoft.com/zh-cn/visualstudio/code-quality/install-fxcop-analyzers?view= ...

  4. 网络编程 TCP协议:三次握手,四次回收,反馈机制 socket套接字通信 粘包问题与解决方法

    TCP协议:传输协议,基于端口工作 三次握手,四次挥手 TCP协议建立双向通道. 三次握手, 建连接: 1:客户端向服务端发送建立连接的请求 2:服务端返回收到请求的信息给客户端,并且发送往客户端建立 ...

  5. P1908 逆序对-(树状数组)

    https://www.luogu.org/problem/P1908 比较喜欢线段树,懒得用树状数组(只会套模板,位运算的精髓没有领悟到),一直没有记录树状数组代码,又得捡回来,趁这道题记录一下模板 ...

  6. 【oracle】update 某字段为null

    字段 = null 注意这个字段要可以为空

  7. [NOI2009][codevs1846]KCOJ0191]植物大战僵尸

    题目描述 Description Plants vs. Zombies(PVZ)是最近十分风靡的一款小游戏.Plants(植物)和Zombies(僵尸)是游戏的主角,其中Plants防守,而Zombi ...

  8. ubunt 文件permission denied问题的解决

    在linux系统使用过程中,升级python到3.6以后,执行pip命令,遇到permission denied问题,系统显示如下: -bash: /home/www/my_flask/venv/bi ...

  9. 别再说你不会 ElasticSearch 调优了,都给你整理好了

    来源:http://tinyurl.com/y4gnzbje 第一部分:调优索引速度 第二部分-调优搜索速度 第三部分:通用的一些建议 英文原文:https://www.elastic.co/guid ...

  10. 【06月05日】A股滚动市净率PB历史新低排名

    2010年01月01日 到 2019年06月05日 之间,滚动市净率历史新低排名. 上市三年以上的公司,2019年06月05日市净率在30以下的公司. 来源:A股滚动市净率(PB)历史新低排名. 1 ...