【LeetCode】1120. Maximum Average Subtree 解题报告 (C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址: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:
- The number of nodes in the tree is between 1 and 5000.
- Each node will have a value between 0 and 100000.
- Answers will be accepted as correct if they are within 10^-5 of the correct answer.
题目大意
给出一个二进制数组 data,你需要通过交换位置,将数组中 任何位置 上的 1 组合到一起,并返回所有可能中所需 最少的交换次数。
解题方法
DFS
- 给每个节点定义一个
pair<int, int>
,第一个位置表示以该节点为根的子树值的和,第二个位置表示子树的节点数; - 自顶向上的累加每个节点的这两个数值;
- 子树平均数是和/节点,使用一个全局变量来存储;
- 使用字典做记忆化搜索,用来加速
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++)的更多相关文章
- [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 ...
- 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)
[LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...
- LeetCode 2 Add Two Sum 解题报告
LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】870. Advantage Shuffle 解题报告(Python)
[LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
随机推荐
- R包开发过程记录
目的 走一遍R包开发过程,并发布到Github上使用. 步骤 1. 创建R包框架 Rsutdio --> File--> New Project--> New Directory - ...
- 【Python小试】统计一条核酸序列中频数非0或为2的双核苷酸
概念 双核苷酸由任意2个碱基组成 测试1 dna = "AATGATGAACGAC" #一一列举 dinucleotides = ['AA','AT','AG','AC', 'TA ...
- linux vi和vim编辑器
所有的Linux系统都会内建vi文本编辑器,vim具有程序编辑的能力,可以看作是vi的增强版本 三种常见模式 正常模式 以vim打开一个文档直接进入的模式,快捷键可以使用. 1.这个模式可以使用上下左 ...
- 大数据学习day11------hbase_day01----1. zk的监控机制,2动态感知服务上下线案例 3.HDFS-HA的高可用基本的工作原理 4. HDFS-HA的配置详解 5. HBASE(简介,安装,shell客户端,java客户端)
1. ZK的监控机制 1.1 监听数据的变化 (1)监听一次 public class ChangeDataWacher { public static void main(String[] arg ...
- vim一键整理代码命令
vim下写代码超实用代码格式整理命令,仅需四步 ①先使用 gg 命令使光标回到第一行 ②shift+v 进入可视模式 ③shift+g 全选 ④按下 = 即可 混乱的代码格式 四步整理以后 工整又 ...
- AI常用环境安装
torch环境 conda create --name py37 python=3.7 conda activate py37 pip install jieba==0.42.1pip install ...
- 3.6 String 与 切片&str的区别
The rust String is a growable, mutable, owned, UTF-8 encoded string type. &str ,切片,是按UTF-8编码对St ...
- python web框架学习笔记
一.web框架本质 1.基于socket,自己处理请求 #!/usr/bin/env python3 #coding:utf8 import socket def handle_request(cli ...
- File类及常用操作方法
import java.io.File; import java.io.IOException; public class file { public static void main(String[ ...
- 【Java多线程】线程池-ThreadPoolExecutor
ThreadPoolExecutor提供了四个构造方法: 我们以最后一个构造方法(参数最多的那个),对其参数进行解释: public ThreadPoolExecutor(int corePoolSi ...