[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.)
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:
- The number of nodes in the tree is between
1and5000. - Each node will have a value between
0and100000. - Answers will be accepted as correct if they are within
10^-5of 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的更多相关文章
- 【LeetCode】1120. Maximum Average Subtree 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...
- LeetCode 643. Maximum Average Subarray I (最大平均值子数组之一)
Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...
- [LeetCode] 644. Maximum Average Subarray II 子数组的最大平均值之二
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- [Leetcode]643. Maximum Average Subarray I
Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...
- [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 ...
- leetcode 643. Maximum Average Subarray I 子数组最大平均数 I
一.题目大意 https://leetcode.cn/problems/maximum-average-subarray-i/ 给你一个由 n 个元素组成的整数数组 nums 和一个整数 k . 请你 ...
- [LeetCode] Maximum Average Subarray II 子数组的最大平均值之二
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- [LeetCode] Maximum Average Subarray I 子数组的最大平均值
Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...
- LeetCode Maximum Average Subarray I
原题链接在这里:https://leetcode.com/problems/maximum-average-subarray-i/description/ 题目: Given an array con ...
随机推荐
- websocket 的基本用法
项目当中使用到了websocket,以前的项目当中使用到了另外一个类似的socket.io,两者的区别和联系在另外一篇文章当中有提及,这里就简单的写下websocket的用法 下面的例子是阮一峰的We ...
- UiPath: Selectors repair 选择器的修复,即被选择的按钮发生改变如何选择第二按钮
实现批量注册用户功能时,出现第一个用户注册完时,弹出确认按钮,点击即可,但是第二个用户注册完成时,弹出的按钮与第一个有差异,图形用户界面元素及其父元素的属性都发生改变.所以就点不了按钮,就卡死在这.如 ...
- nginx 静态资源服务
1.文件压缩 location ~ .*\.(jpg|gif|png)$ { gzip on(开启); gzip_http_version 1.1(版本); gzip_comp_level 2(压缩比 ...
- 20180706模拟赛T2——染色
文件名: seq 题目类型: 传统题 时间限制: 1秒 内存限制: 128MB 编译优化: 无 题目描述 小A正在帮助小M刷她家的墙壁 小M家的墙可以分为\(n\)块,每段需要被刷成黑色或者白色.你可 ...
- USACO Chocolate Giving
洛谷 P2984 [USACO10FEB]给巧克力Chocolate Giving 洛谷传送门 JDOJ 2680: USACO 2010 Feb Silver 2.Chocolate Giving ...
- Rpm Creating Subpackages
转自:http://ftp.rpm.org/max-rpm/s1-rpm-subpack-spec-file-changes.html Spec File Changes For Subpackage ...
- ABP 往前端返回详细的错误信息
在这个类:MyABP.Web.Startup.MyABPWebMvcModule 中 的 PreInitialize 方法 添加一句: Configuration.Modules.AbpWebComm ...
- Vim Python3环境打造
Vim Python3环境打造 tags: Vim Python3 参考网址:Vim与Python真乃天作之合:打造强大的Python开发环境 分割布局 sv 纵向分割 vs 横向分割 ctrl+W ...
- Flask常用路由参数
Flask中的路由参数: @app.route(‘/’, endpoint=’xx’ , methods=[‘GET’,...]) >endpoint后的名字,用来反向生成url的.后面的名字随 ...
- Spring Boot启动命令参数详解及源码分析
使用过Spring Boot,我们都知道通过java -jar可以快速启动Spring Boot项目.同时,也可以通过在执行jar -jar时传递参数来进行配置.本文带大家系统的了解一下Spring ...