题目地址:https://leetcode-cn.com/problems/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:

  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.

题目大意

给出一个二进制数组 data,你需要通过交换位置,将数组中 任何位置 上的 1 组合到一起,并返回所有可能中所需 最少的交换次数。

解题方法

DFS

  1. 给每个节点定义一个pair<int, int>,第一个位置表示以该节点为根的子树值的和,第二个位置表示子树的节点数;
  2. 自顶向上的累加每个节点的这两个数值;
  3. 子树平均数是和/节点,使用一个全局变量来存储;
  4. 使用字典做记忆化搜索,用来加速

C++代码如下:

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
double maximumAverageSubtree(TreeNode* root) {
double res = -1;
dfs(root, res);
return res;
}
pair<int, int> dfs(TreeNode* root, double& min_avg) {
if (!root) return {0, 0};
if (m_.count(root)) return m_[root];
pair<int, int> left = dfs(root->left, min_avg);
pair<int, int> right = dfs(root->right, min_avg);
pair<int, int> cur;
cur.first += left.first + right.first + root->val;
cur.second += left.second + right.second + 1;
min_avg = max(min_avg, (double)cur.first / cur.second);
m_[root] = cur;
return cur;
}
private:
// 节点 : 子树的和,子树的节点数
unordered_map<TreeNode*, pair<int, int>> m_;
};

日期

2019 年 9 月 23 日 —— 昨夜睡的早,错过了北京的烟火

【LeetCode】1120. Maximum Average Subtree 解题报告 (C++)的更多相关文章

  1. [Leetcode] 1120. Maximum Average Subtree

    Given the root of a binary tree, find the maximum average value of any subtree of that tree. (A subt ...

  2. 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)

    [LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...

  3. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  4. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  5. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  6. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  7. 【LeetCode】886. Possible Bipartition 解题报告(Python)

    [LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  8. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  9. 【LeetCode】870. Advantage Shuffle 解题报告(Python)

    [LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

随机推荐

  1. 【豆科基因组】普通豆/菜豆/四季豆Common bean (Phaseolus vulgaris L.) 基因组

    目录 研究一:G19833组装,2014NG 研究二:BAT 93组装,2016 genome biology 菜豆属(Phaseolus L.)为同源二倍体作物,包含有80 多个物种,多数为野生种, ...

  2. GlimmerHMM指南

    GlimmerHMM指南 官方用户手册 GlimmerHMM是一种De novo的新基因预测软件. 新基因发现基于Generalized Hidden Markov Model (GHMM). Gli ...

  3. SPI详解2

    串行外设接口 (SPI) 总线是一种运行于全双工模式下的同步串行数据链路.用于在单个主节点和一个或多个从节点之间交换数据. SPI 总线实施简单,仅使用四条数据信号线和控制信号线(请参见图 1). 图 ...

  4. 求解线性递推方程第n项的一般方法

    概述 系数为常数,递推项系数均为一次的,形如下面形式的递推式,称为线性递推方程. \[f[n]=\begin{cases} C &n\in Value\\ a_1 f[n-1]+a_2 f[n ...

  5. C#时间选择

    <script type="text/javascript" src="http://www.shicishu.com/down/WdatePicker.js&qu ...

  6. 振鹏学习Java的第二天!

    一.今日收获 1.了解了eclipse的具体使用方法. 2.学习了Java程序设计完全手册的第一章内容,明白了相关知识. 3.通过看哔哩哔哩的java的教程视频了解了Dos命令及java的变量和常量. ...

  7. 扩展kmp 学习笔记

    学习了一下这个较为冷门的知识,由于从日报开始看起,还是比较绕的-- 首先定义 \(Z\) 函数表示后缀 \(i\) 与整个串的 \(lcp\) 长度 一个比较好的理解于实现方式是类似于 \(manac ...

  8. Hadoop org.apache.hadoop.util.DiskChecker$DiskErrorException问题等价解决linux磁盘不足解决问题排查

    org.apache.hadoop.util.DiskChecker$DiskErrorException问题等价解决linux磁盘不足解决问题排查 解决"/dev/mapper/cento ...

  9. Java、Scala获取Class实例

    Java获取Class实例的四种方式 package com.test; /** * @description: TODO * @author: HaoWu * @create: 2020/7/22 ...

  10. linux shell中的条件判断语句

    http://bbs.chinaunix.net/thread-396805-1-1.html shell 判断语句 流程控制 "if" 表达式 如果条件为真则执行then后面的部 ...