diagonal sum in Binary Tree Problem Solution -
In this blog we will see How to print Diagonal Sum In Binary Tree| diagonal sum in Binary Tree Problem Solution . It is most common question related to recursion lets read diagonal sum in Binary Tree Problem.
Let's Jump To my Solution now-
After a deep observation, I divided this binary tree having n diagonal and I will write a function that will calculate all diagonal sum and store solution in a map.
for example, diagonal number 1 ans will be stored in a map having key 1.
see the image below
Fucntion Descreption ->
I have written a function that returns the sum of diagonal if I pass the current root, map(for storing diagonal sum till now ), and diagonal number to this function parameter. This is a magical function, isn't it 😆😆 XD.
Now I have to find the smallest possible case which will help me to write base case of my problem
if the root doesn't exist I have to simply return because the tree is empty.
Now I found the smallest problem Lets write base case.
Now How to write the next block of code.
Think like that -
I don't know the solution but if somehow i get diagonal sum excluded root node my task will be minimized and i will add only root node to previous sum.
`At the same time, I noticed that whenever I go to the left of my root node every time my diagonal number increased by 1. By Using this Property I can write my diagonal sum in Binary Tree Problem Solution.
so Let's write the induction step now if you think like that it will very easy to write induction step-
Hopefully, You understood the entire solution if not comment below.
Here is the complete code of my solution -