题目地址: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.
(Asubtree 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、 Thenumberofnodesinthetreeisbetween1and5000.;
2、 Eachnodewillhaveavaluebetween0and100000.;
3、 Answerswillbeacceptedascorrectiftheyarewithin10^-5ofthecorrectanswer.;
题目大意
给出一个二进制数组 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_;
};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
2022
DDKK.COM 弟弟快看-教程,程序员编程资料站,版权归原作者所有
本文经作者:负雪明烛 授权发布,任何组织或个人未经作者授权不得转发