[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 ...
随机推荐
- ggplot2学习笔记之图形排列
转载:https://www.jianshu.com/p/d46cf6934a2f R语言基本绘图函数中可以利用par()以及layout()来进行图形排列,但是这两个函数对于ggplot图则不太适用 ...
- 使用acme.sh申请&自动续期LetsEncrypt免费SSL证书(转)
一.简介 LetsEncrypt是一个免费.自动.开放的证书颁发机构.acme.sh 实现了 acme 协议, 可以从 LetsEncrypt 生成免费的证书. 本文介绍如何使用acme.sh来签发并 ...
- POJ3070 斐波那契数列递推 矩阵快速幂模板题
题目分析: 对于给出的n,求出斐波那契数列第n项的最后4为数,当n很大的时候,普通的递推会超时,这里介绍用矩阵快速幂解决当递推次数很大时的结果,这里矩阵已经给出,直接计算即可 #include< ...
- bert模型参数简化
我们下载下来的预训练的bert模型的大小大概是400M左右,但是我们自己预训练的bert模型,或者是我们在开源的bert模型上fine-tuning之后的模型的大小大约是1.1G,我们来看看到底是什么 ...
- Latex使用过程中的一些总结
本文主要总结在使用Latex过程中遇到的一些问题及解决方案. 一:关于参考文献 1.如何在paper同一处用\cite命令同时引用多篇文献? 用\cite{bibtex1}\cite{bibtex2} ...
- Python 字符串内置方法笔记
一. 大小写转换 ① capitalize() capitalize() #字符串首字母大写 >>> str0 = 'hello World' >>> str0.c ...
- NGUI里的sprite和label有白色的边框
问题描述:NGUI里的sprite和label有白色的边框,而原图一切正常 如图: 解决方案: 给Sprite 边缘左右更增加1,这样拉伸的时候就忽略了左右1的位置,图片就不会显示白色边框了
- DFS_BFS(深度优先搜索 和 广度优先搜索)
package com.rao.graph; import java.util.LinkedList; /** * @author Srao * @className BFS_DFS * @date ...
- Pandas | 27 注意事项&窍门
警告和疑难意味着一个看不见的问题.在使用Pandas过程中,需要特别注意的地方. 与Pandas一起使用If/Truth语句 当尝试将某些东西转换成布尔值时,Pandas遵循了一个错误的惯例. 这种情 ...
- PATA1028 List Sorting
Excel can sort records according to any column. Now you are supposed to imitate this function. Input ...