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调用函数设置超时机制

    有时候需要给函数设置超时机制,以防止它卡住我们的程序,这里可以用python的signal模块,signal模块可以实现程序内部的信号处理. # coding:utf8 import time imp ...

  2. eclipse 搭建springboot项目pom.xml报错

    1. 报错信息 2. 解决方法 在pom.xml文件中加入maven版本修改 <maven-jar-plugin.version>3.1.1</maven-jar-plugin.ve ...

  3. Apache Flink流式处理

    花了四小时,看完Flink的内容,基本了解了原理. 挖个坑,待总结后填一下. 2019-06-02 01:22:57等欧冠决赛中,填坑. 一.概述 storm最大的特点是快,它的实时性非常好(毫秒级延 ...

  4. PS下修改背景色

    1.打开要修改的图片 2.选择左侧的快速选择工具,右键选择魔法棒 3.在图片上点击左键选取背景 4.菜单栏选择编辑,点击填充 5.在填充选项框中选择“颜色",点击选取要使用的颜色,确定

  5. MySol序

    1.mysql是基于C/S端的服务器软件 mysql服务端: server端开启,对文件的增删改查 , musql客户端::连接S端, 发送指令 isnert into t1 values(1); 2 ...

  6. 关于std::bind的文章收集

    C++11 FAQ中文版:std::function 和 std::bind 2011-03-02 16:25 by 陈良乔 常规性地介绍了function和bind的使用,还不会用的同学可以看看 b ...

  7. Linux——查找占用磁盘体积最大的前10个文件

    前言 服务器上传文件失败了,才开始没考虑到磁盘原因还以为是自己的scrt的问题,还好df -h看了下,最后发现磁盘满了,真是.... 查找 find / -type f -print0 | xargs ...

  8. Git创建与简单使用

    一. 服务器端 创建空的仓库(以项目tm201为例) 1. git账户登录 2. 新建仓库目录 mkdir tm101.git && cd tm201.git 3. git初始化新的空 ...

  9. 破解优酷VIP视频

    目录 一 破解优酷VIP视频 一 破解优酷VIP视频 import requests import re import json HEADERS = { 'user-agent': 'Mozilla/ ...

  10. Margin和padding失效

    太久不写原生果然不行,Margin和padding对div有效,对span失效,原因就不解释了(元素性质,块状之类的)