Solution to a fairly tricky question I was just asked during an interview. Initially I thought the best solution was to do recursive sums at each node as a head. That would work, but as my interviewer pointed out, there was a more efficient way to do the same thing. Thought about it while waiting for my plane just now and this is what I came up with. This one is definitely of lower complexity than the last one (which would be more like O(n^2) ) - computing the sums here is: O( n log n )w
Wrote an indexed MinPQ. Idea here is to be able to maintain an array of
elements and instead of moving the elements around like we usually do in
 a min-oriented PQ, we will instead move the indexes around instead.
This turns out to be useful in situations like Dijkstra' shortest path,
where we might want to insert edge costs into a priority queue and
remove the one that corresponds to the lowest cost from the list first.