[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
1
and5000
. - Each node will have a value between
0
and100000
. - 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的更多相关文章
- 【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 ...
随机推荐
- 搭建Ceph分布式存储
环境: 系统 IP地址 主机名(登录用户) 承载角色 Centos 7.4 64Bit 1611 10.199.100.170 dlp(yzyu) ceph-client(root) admin-no ...
- django 基础1
1.web应用 本质是基于socket实现的应用程序 浏览器---------服务器 2.http协议:应用层协议 1.基于TCP协议 2.基于请求响应 3.短连接 4.无状态 请求协议 浏览器--- ...
- No qualifying bean of type 'com.chinanums.agent.operation.service.component.OperationPageComponent' available:
java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.conte ...
- centos安装安全狗5步就能完成
安全狗是为服务器开发的一款服务器管理软件,客户的服务器是centos 64位,我们就来看看如何安装吧.首先必须得有root账号权限,不然下面的步骤可能都无法执行.随ytkah一起来操作吧 1.下载安全 ...
- scrapy框架--?乱码unicode
安装 pip install scrapy 建立一个爬虫项目 scrapy startproject 项目名称 scrapy startproject itcast 进入itcast文件夹 生成一个爬 ...
- Python GIL、CPU密集型、IO密集型
Python GIL(Global Interpreter Lock(全局解释器锁)) 1:进程里面多个线程,线程 共享A=10 2:Python解释器,A改完值之后会传回进程容器,为了防止A和B同时 ...
- Scrapy笔记02- 完整示例
Scrapy笔记02- 完整示例 这篇文章我们通过一个比较完整的例子来教你使用Scrapy,我选择爬取虎嗅网首页的新闻列表. 这里我们将完成如下几个步骤: 创建一个新的Scrapy工程 定义你所需要要 ...
- Pandas | 22 时间差
时间差(Timedelta)是时间上的差异,以不同的单位来表示.例如:日,小时,分钟,秒.它们可以是正值,也可以是负值.可以使用各种参数创建Timedelta对象,如下所示 - 字符串 通过传递字符串 ...
- 【可持久化0/1Trie】【P4735】最大异或和
Description 给定一个长度为 \(n\) 的序列 \(A\),有 \(m\) 次操作,每次要么在序列尾部再添加一个数,将序列长度 \(n\) 加一,要么给进行一次查询,给定查询参数 \(l, ...
- jQuery事件绑定与切换
一.事件绑定 1.标准方式 1. jquery标准的绑定方式 * jq对象.事件方法(回调函数): * 注:如果调用事件方法,不传递回调函数,则会触发浏览器默认行为. * 表单对象.submit(); ...