N ary tree - averages
Anonymous User
667

You are given a n-tree, you need to calculate the averages of all the children and itself. You need to get the parent node average including its children that is the higest.

image

In the tree above the highest average is 11 (12+10/2) for a branch in middle . For node 10 at the root the average is 7.2 so it is not highest. This is easy part,do dfs and run averages at each level.

What is the efficient way to store this tree and its averages if there are frequent changes to the node values and addition of more nodes? And you will be requested for the highest average child frequently after the change to the tree.
Another follow up, how do you efficiently know the highest average child at a particular level and below it. (not considering the whole tree).

You can imagine that the tree is very large.

Comments (2)