Midway91 / HactoberFest2023

This is for HacktoberFest2023
135 stars 1.82k forks source link

Create Diagonal sum of Binary tree Solution #1279

Closed MUSKAN-GARG1108 closed 11 months ago

MUSKAN-GARG1108 commented 11 months ago

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.