Subtree with Maximum Average

2023-12-23 22:38
文章标签 maximum average subtree

本文主要是介绍Subtree with Maximum Average,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Given a binary tree, find the subtree with maximum average. Return the root of the subtree.

python:一定要注意,除法的精度问题。leetcode中不一定使用的是python3.X

"""
Definition of TreeNode:
class TreeNode:def __init__(self, val):this.val = valthis.left, this.right = None, None
"""
class Solution:# @param {TreeNode} root the root of binary tree# @return {TreeNode} the root of the maximum average of subtreeavg, node = 0, Nonedef findSubtree2(self, root):# Write your code hereif root is None:return Noneself.getSubtree(root)return self.node;def getSubtree(self, root):if root is None:return 0, 0sumLeft, countLeft = self.getSubtree(root.left)sumRight, countRight = self.getSubtree(root.right)sumRoot = sumLeft + sumRight + root.valcountRoot = countLeft + countRight + 1average = sumRoot * 1.0 / countRoot if self.node is None or average > self.avg:self.avg = averageself.node = rootreturn sumRoot, countRoot


Java

/*** Definition of TreeNode:* public class TreeNode {*     public int val;*     public TreeNode left, right;*     public TreeNode(int val) {*         this.val = val;*         this.left = this.right = null;*     }* }*/
class ResultType {public int sum;public int count;public double avg;public TreeNode node;public ResultType(int sum, int count, double avg, TreeNode node) {this.sum = sum;this.count = count;this.node = node;this.avg = avg;}
}
public class Solution {/*** @param root the root of binary tree* @return the root of the maximum average of subtree*/private double avger = Integer.MIN_VALUE;private TreeNode node = null;public TreeNode findSubtree2(TreeNode root) {// Write your code hereif (root == null) {return null;}getSubtree(root);return node;}private ResultType getSubtree(TreeNode root) {if (root == null) {return new ResultType(0, 0, 0, null);}ResultType left = getSubtree(root.left);ResultType right = getSubtree(root.right);int sum = root.val + left.sum + right.sum;int num = 1 + left.count + right.count;if (avger < (double)sum / num) {avger = (double)sum / num;node = root;}return new ResultType(sum, num, (double)sum / num, root);}
}



这篇关于Subtree with Maximum Average的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/529705

相关文章

leetcode#628. Maximum Product of Three Numbers

题目 Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3]Output: 6 Example 2: Input: [1,2,3,4]Output: 24 Note: The lengt

Maximum likelihood function maximizes what thing?

最大似然函数(Maximum Likelihood Function)最大化的是数据在给定参数下出现的概率。具体来说,它最大化的是似然函数(Likelihood Function),即给定参数 ( \theta ) 下观测数据的概率。在统计学中,似然函数 ( L(\theta) ) 通常定义为所有独立观测数据点概率的乘积,对于参数 ( \theta ) 的函数。 对于一组独立同分布的观测数据

ORA-24067: exceeded maximum number of subscribers for queue ADMIN.SMS_MT_QUEUE

临时处理办法: delete from aq$_ss_MT_tab_D;delete from aq$_ss_MT_tab_g;delete from aq$_ss_MT_tab_h;delete from aq$_ss_MT_tab_i;delete from aq$_ss_MT_tab_p;delete from aq$_ss_MT_tab_s;delete from aq$

[LeetCode] 239. Sliding Window Maximum

题:https://leetcode.com/problems/sliding-window-maximum/description/ 题目 Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You

vue3 el-menu 菜单Maximum recursive updates exceeded 报错

vue3 用el-menu实现管理后台左侧菜单,报Uncaught (in promise) Maximum recursive updates exceeded in component <ElMenu>. This means you have a reactive effect that is mutating its own dependencies and thus recursivel

浙大数据结构:01-复杂度2 Maximum Subsequence Sum

数据结构MOOC PTA习题 01-复杂度2 Maximum Subsequence Sum #include <iostream>using namespace std;const int M = 100005;int a[M];int main(){int k;cin >> k;int f = 1;for (int i = 0; i < k; i++){cin >> a[i

Maximum Index

Given an array arr[], find the maximum j – i such that arr[j] > arr[i] Given an array arr[], find the maximum j – i such that arr[j] > arr[i]. Examples: Input: {34, 8, 10, 3, 2, 80, 30, 33, 1}O

Maximum Number in Mountain Sequence

Given a mountain sequence of n integers which increase firstly and then decrease, find the mountain top. Example Example 1: Input: nums = [1, 2, 4, 8, 6, 3] Output: 8 Example 2: Input: nums = [

Maximum Depth of N-ary Tree

Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]Output: 5 思路1:DFS ,divide and conquer /*// Definition for a Node.class Node {public int v

Average of Levels in Binary Tree

Input:3/ \9 20/ \15 7Output: [3, 14.5, 11]Explanation:The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11. Hence return [3, 14.5, 11]. 思路:就是一个level order trav