题目地址: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. python-django使用ORM模型增删改查CRUD

    from weibo.models import WeiboUser as User user_obj = User.objects.get(pk=1) user_obj.pk Out[4]: 1 u ...

  2. 34. Swap Nodes in Pairs

    Swap Nodes in Pairs My Submissions QuestionEditorial Solution Total Accepted: 95230 Total Submission ...

  3. 基于Kubernetes实现前后端应用的金丝雀发布

    基于Kubernetes实现前后端应用的金丝雀发布 公司的研发管理平台实现了Gitlab+Kubernetes的Devops,在ToB和ToC场景中,由于用户量大,且预发布环境和生产环境或多或少存在差 ...

  4. c/c++在线编译Output Limit Exceeded(OLE)错误

    提示输出错误,有如下两个可能情况: 1. 不符合题目给出的输出格式,自己输出了多余的内容或者格式不正确 2. 输入数据的时候,未考虑到输入错误的情况 针对2,有如下的例子: 错误的情况: 1 int ...

  5. HDFS【Namenode、SecondaryNamenode、Datanode】

    目录 一. NameNode和SecondaryNameNode 1.NN和2NN 工作机制 2. NN和2NN中的fsimage.edits分析 3.checkpoint设置 4.namenode故 ...

  6. C++ 之杂记

    今天做了一个题,代码不难,但是编译的时候就恼火,老是报错,也不告诉我错哪了.... 之前的代码是这样的,在main函数中调用这个类的构造函数,就一直报错,但是不知道原因,后来加上了const 就好了. ...

  7. 为什么CTR预估使用AUC来评估模型?

    ctr预估简单的解释就是预测用户的点击item的概率.为什么一个回归的问题需要使用分类的方法来评估,这真是一个好问题,尝试从下面几个关键问题去回答. 1.ctr预估是特殊的回归问题 ctr预估的目标函 ...

  8. 【Linux】【Basis】用户、组和权限管理

    1. 概念: 1.1. 每个使用者都有,用户标识UID和密码:并且有身份(Authentication),授权(Authorization),审计(Audition):组(用户组,用户容器) 1.2. ...

  9. String类型和包装类型作为参数传递时,是属于值传递还是引用传递呢?

    原理知识: 如果参数类型是原始类型,那么传过来的就是这个参数的一个副本,也就是这个原始参数的值,这个跟之前所谈的传值是一样的.如果在函数中改变了副本的 值不会改变原始的值. 如果参数类型是引用类型,那 ...

  10. 【Java多线程】CompletionService

    什么是CompletionService? 当我们使用ExecutorService启动多个Callable时,每个Callable返回一个Future,而当我们执行Future的get方法获取结果时 ...