标签:right val 1120 average Maximum number root Average left
Given the root
of a binary tree, return the maximum average value of a subtree of that tree. Answers within 10-5
of the actual answer will be accepted.
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: root = [5,6,1] Output: 6.00000 Explanation: For the node with value = 5 we have an average of (5 + 6 + 1) / 3 = 4. For the node with value = 6 we have an average of 6 / 1 = 6. For the node with value = 1 we have an average of 1 / 1 = 1. So the answer is 6 which is the maximum.
Example 2:
Input: root = [0,null,1] Output: 1.00000
还是dc的模板,但是
是从这里递归出去的,所以可以用+1来计算子节点的数量
int number = left + right + 1;
https://leetcode.com/problems/maximum-average-subtree/discuss/715694/Java-0ms-simple
class Solution { private double average = 0; public double maximumAverageSubtree(TreeNode root) { helper(root); return average; } private int helper(TreeNode root){ if(root == null) return 0; int left = helper(root.left); int right = helper(root.right); int number = left + right + 1; if(left != 0) root.val += root.left.val; if(right != 0) root.val += root.right.val; average = Math.max(average, (double)(root.val) / number); return number; } }
标签:right,val,1120,average,Maximum,number,root,Average,left 来源: https://www.cnblogs.com/immiao0319/p/15201690.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。