Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Maximum Average Subtree in Pytho
Given the root of a binary tree, we need to find the maximum average value of any subtree in that tree. A subtree's average is calculated by dividing the sum of all nodes in the subtree by the count of nodes.
For the example tree above ?
Node 5 subtree: (5 + 6 + 1) / 3 = 4.0
Node 6 subtree: 6 / 1 = 6.0
Node 1 subtree: 1 / 1 = 1.0
The maximum average is 6.0 from the subtree rooted at node 6.
Algorithm
We use a recursive approach that returns both the sum and count of nodes for each subtree ?
For each node, recursively calculate sum and count of left and right subtrees
Calculate current subtree's sum and count
Update the maximum average found so far
Return the sum and count to parent node
Implementation
class TreeNode:
def __init__(self, data, left=None, right=None):
self.data = data
self.left = left
self.right = right
def insert(temp, data):
queue = []
queue.append(temp)
while queue:
temp = queue.pop(0)
if not temp.left:
if data is not None:
temp.left = TreeNode(data)
else:
temp.left = TreeNode(0)
break
else:
queue.append(temp.left)
if not temp.right:
if data is not None:
temp.right = TreeNode(data)
else:
temp.right = TreeNode(0)
break
else:
queue.append(temp.right)
def make_tree(elements):
tree = TreeNode(elements[0])
for element in elements[1:]:
insert(tree, element)
return tree
class Solution:
def helper(self, node):
if not node:
return 0, 0
left_sum, left_count = self.helper(node.left)
right_sum, right_count = self.helper(node.right)
total_sum = left_sum + right_sum + node.data
total_count = left_count + right_count + 1
current_average = total_sum / total_count
self.max_average = max(self.max_average, current_average)
return total_sum, total_count
def maximumAverageSubtree(self, root):
self.max_average = 0
self.helper(root)
return self.max_average
# Example usage
ob = Solution()
root = make_tree([5, 6, 1])
result = ob.maximumAverageSubtree(root)
print(f"Maximum average subtree: {result}")
Maximum average subtree: 6.0
How It Works
The algorithm performs a post-order traversal of the binary tree. For each node, it ?
Recursively processes left and right children
Calculates the current subtree's sum and node count
Computes the average for the current subtree
Updates the global maximum average if current is larger
Returns sum and count to the parent for its calculations
Time and Space Complexity
Time Complexity: O(n) where n is the number of nodes, as we visit each node once
Space Complexity: O(h) where h is the height of the tree due to recursion stack
Conclusion
This solution efficiently finds the maximum average subtree using a single traversal. The key insight is using post-order traversal to calculate subtree sums and counts bottom-up while tracking the maximum average encountered.
