'I have a problem with calculating SUM amongst nodes in the subgraph of binary tree

I have a problem with calculating SUM amongst nodes in the subgraph. If I have a binary tree then I need to sum a certain property namely "weight" on nodes. for example, I should SUM all weight on the right side of specific nodes. Next Problem is, that 10 levels (depth) after that node in this path formula will be changed. So I need to return all SUM weight at every level. For instance, is my aim output:

  1. List item_levels____|sum of weights__
  2. 1 | 16
  3. 2 | 7

..... and so on



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source