The idea is to keep track of vertical distance from top diagonal passing through the root. We increment the vertical distance we go down to next diagonal.
Add root with vertical distance as 0 to the queue. Process the sum of all right child and right of the right child and so on. Add left child current node into the queue for later processing. The vertical distance of the left child is the vertical distance of current node plus 1. Keep doing 2nd, 3rd and 4th step till the queue is empty.
Algorithm:
The idea is to keep track of vertical distance from top diagonal passing through the root. We increment the vertical distance we go down to next diagonal.
Add root with vertical distance as 0 to the queue. Process the sum of all right child and right of the right child and so on. Add left child current node into the queue for later processing. The vertical distance of the left child is the vertical distance of current node plus 1. Keep doing 2nd, 3rd and 4th step till the queue is empty.