Given a tree ofnnodes. Theithnode's father isfa[i-1]and the value of theithnode isval[i-1]. Especially,1represents the root,2represents the second node and so on. We gurantee that-1is the father of root(the first node) which means thatfa[0] = -1.
The average value of a subtree is the result of the sum of all nodes in the subtree divide by the number of nodes in it.
Find the maximum average value of the subtrees in the given tree, return the number which represents the root of the subtree.
Example
Givenfa=[-1,1,1,2,2,2,3,3], representing the father node of each point, andval=[100,120,80,40,50,60,50,70], representing the value of each node, return1.
class Solution:
"""
@param fa: the father
@param val: the val
@return: the biggest node
"""
maxAvg = float('-inf')
maxI = -1
def treeProblem(self, fa, val):
# Write your code here
n = len(fa)
if not n:
return -1
graph = {i: [] for i in range(1, n + 1)}
graph.update({-1: []})
for i in range(1, n + 1):
graph[fa[i - 1]].append(i)
map = {}
self.dfs(graph,-1,val,map)
return self.maxI
def dfs(self,graph,i,val,map):
if i in map:
return map[i]
cnt = 1 if i != -1 else 0
sum = val[i-1] if i != -1 else 0
for child in graph[i]:
cres = self.dfs(graph,child,val,map)
cnt += cres[0]
sum += cres[1]
avg = sum/cnt
if avg > self.maxAvg:
self.maxAvg = avg
self.maxI = i
map[i] = [cnt,sum]
return map[i]