The Maximum Average Subtree of a Binary Tree

  • 时间:2020-09-23 15:50:46
  • 分类:网络文摘
  • 阅读:95 次

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:

  5
 / \
6   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.

Depth First Search Algorithm to Compute the Maximum SubTree Average

Similar problem: How to Find out the Most Frequent Subtree Sum using Depth First Search Algorithm?

To compute the average, we need the sum and as well as the number of nodes. We use the second parameter (reference) to pass down the counter. And the Depth First Search function will return the sum for the subtree. As we compute the average, we can record the maximum.

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
/**
 * 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) {
        int cnt = 0;
        getSum(root, cnt);
        return maxAvg;
    }
    
private:
    int getSum(TreeNode* root, int &cnt) {
        if (root == nullptr) return 0;
        int left_cnt = 0;
        int right_cnt = 0;
        int left = getSum(root->left, left_cnt);
        int right = getSum(root->right, right_cnt);
        int sum = left + right + root->val;
        cnt = left_cnt + right_cnt + 1;
        maxAvg = max(maxAvg, (double)sum / cnt);
        return sum;
    }
    double maxAvg = 0.0;
};
/**
 * 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) {
        int cnt = 0;
        getSum(root, cnt);
        return maxAvg;
    }
    
private:
    int getSum(TreeNode* root, int &cnt) {
        if (root == nullptr) return 0;
        int left_cnt = 0;
        int right_cnt = 0;
        int left = getSum(root->left, left_cnt);
        int right = getSum(root->right, right_cnt);
        int sum = left + right + root->val;
        cnt = left_cnt + right_cnt + 1;
        maxAvg = max(maxAvg, (double)sum / cnt);
        return sum;
    }
    double maxAvg = 0.0;
};

The time and space complexity is O(N) as we are using recursion (implicitly using stacks) to conduct the Depth First Search. The N is the number of the nodes in the tree, which could be degerated to a linked list.

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
日常食物怎样搭配吃出加倍营养?  秋冬季这样吃南瓜可防治便秘胃痛  三种豆子一起吃营养效果最好  红糖对女人健康有三大养生功效  南瓜的养生功效:温润脾胃护心助眠  可以用豆浆替代牛奶来补钙吗?  早餐吃鸡蛋7大好处及快速烹调法  把虾皮作为补钙佳品还需三思而行  饮食健康:保护肝脏必吃8种蔬菜  常吃四种食物可有效排出体内毒素 
评论列表
添加评论