Welcome to the daily solving of our PROBLEM OF THE DAY with Siddharth Hazra. We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Tree but also build up problem-solving skills.
In this problem, we are given Red lines of slope -1 passing between nodes (in the following diagram). The diagonal sum in a binary tree is the sum of all node datas lying between these lines. Given a Binary Tree of size n, print all diagonal sums.
For the following input tree, output should be 9, 19, 42.
9 is sum of 1, 3 and 5.
19 is sum of 2, 6, 4 and 7.
42 is sum of 9, 10, 11 and 12.
DiagonalSum
Example :
Input:
4
/ \
1 3
/
3
Output:
7 4
Give the problem a try before going through the video. All the best!!!
Problem Link: https://www.geeksforgeeks.org/problems/diagonal-sum-in-binary-tree/1