[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 ...
随机推荐
- Linux CentOS 6.5 ifconfig查询不到ip简单解决方法
最近有小伙伴表示在虚拟机中安装CentOS之后使用ifconfig以及ip addr指令无法查询到ip地址, 在此笔者提供一个简单有效的方法; 1. 切换为root用户登录 su root 2.进入配 ...
- 使用acme.sh申请&自动续期LetsEncrypt免费SSL证书(转)
一.简介 LetsEncrypt是一个免费.自动.开放的证书颁发机构.acme.sh 实现了 acme 协议, 可以从 LetsEncrypt 生成免费的证书. 本文介绍如何使用acme.sh来签发并 ...
- 第一部分day2-for、while、数据类型(字符串、列表、元组)
数据类型 数据类型的初识 1.数字 整数 int (integer) 整型 (注:python3 不区分整型和长整型,统一称之为整型) 长整型 float(浮点型) complex(复数) 是由实数和 ...
- php状态模式(state pattern)
... <?php /* The state pattern encapsulates the varying behavior for the same object based on its ...
- 织梦dedecms后台文件media_add.php任意上传漏洞解决办法
织梦在安装到阿里云服务器后阿里云后台会提示media_add.php后台文件任意上传漏洞,引起的文件是后台管理目录下的media_add.php文件,下面跟大家分享一下这个漏洞的修复方法: 首先找到并 ...
- python操作mysql(增、删、改、查)
用python操作数据库,特别是做性能测试造存量数据时特别简单方便,比存储过程方便多了. 连接数据库 前提:安装mysql.python,参考:https://www.cnblogs.com/Uncl ...
- 用JSON.parse(JSON.stringify(itemData))序列化反序列化实现‘深度复制’
还可以用来去除值不具有JSON 表示形式(数字.字符串.逻辑值.数组.对象.null)的属性,也就是说像undefined和function这样的属性值.
- Centos7安装MySQL(多图)
文章目录 一.在线安装1.替换网易yum源2.清理缓存3.下载rpm文件4.安装MySQL数据库二.本地安装1.上传MySQL安装包2.安装依赖的程序包3.卸载mariadb程序包4.安装MySQL程 ...
- plv8 + hashids 生成短连接id
此文章是转载文章的一个学习,稍有改动 环境准备 plv8 环境 version: '3.6' services: postgres: image: dalongrong/plv8:2.3.12 ...
- selenium篇之滑动验证码
一.介绍 现在出现了一种通过用户鼠标移动滑块来填补有缺口图片的验证码,我们叫做滑动验证码.它的原理很简单,首先生成一张图片,然后随机挖去一块,在页面展示被挖去部分的图片,再通过js获取用户滑动距离,以 ...