'Coding has over 700 languages', '67% of programming jobs aren’t in the technology industry', 'Coding is behind almost everything that is powered by electricity', 'Knowing how to code is a major requirement for astronomers', 'The first computer didn’t use any electricity', 'Do you know there is a coding language named “Go“', 'Computer programming is one of the fastest-growing careers', 'Fortran (FORmula TRANslation) was the name of the first programming language', 'The first programmer was the daughter of a mad poet', 'Many programming languages share the same structure', 'Coding will soon be as important as reading', 'How many programmers does it take to change a light bulb? None, that’s a hardware problem', 'Why do Java developers wear glasses? Because they can’t C', 'Software and temples are much the same — first we build them, then we pray', 'An engineer will not call it a bug — it’s an undocumented feature', 'In a room full of top software designers, if two agree on the same thing, that’s a majority', 'C programmers never die. They are just cast into void', 'Knock, knock … Who’s there? … *very long pause* … Java', 'The best thing about a boolean is even if you are wrong, you are only off by a bit', 'Linux is only free if your time has no value', 'The computer was born to solve problems that did not exist before', 'Coding has over 700 languages', '67% of programming jobs aren’t in the technology industry', 'Coding is behind almost everything that is powered by electricity', 'Knowing how to code is a major requirement for astronomers', 'The first computer didn’t use any electricity', 'Do you know there is a coding language named “Go“', 'Computer programming is one of the fastest-growing careers', 'Fortran (FORmula TRANslation) was the name of the first programming language', 'The first programmer was the daughter of a mad poet', 'Many programming languages share the same structure', 'Coding will soon be as important as reading', 'How many programmers does it take to change a light bulb? None, that’s a hardware problem', 'Why do Java developers wear glasses? Because they can’t C', 'Software and temples are much the same — first we build them, then we pray', 'An engineer will not call it a bug — it’s an undocumented feature', 'In a room full of top software designers, if two agree on the same thing, that’s a majority', 'C programmers never die. They are just cast into void', 'Knock, knock … Who’s there? … *very long pause* … Java', 'The best thing about a boolean is even if you are wrong, you are only off by a bit', 'Linux is only free if your time has no value', 'The computer was born to solve problems that did not exist before',
Update appNew update is available. Click here to update.
Topics

Diagonal Sum

Easy
0/40
Average time to solve is 23m
profile
Contributed by
34 upvotes
Asked in companies
OptumPublicis SapientTata Consultancy Services (TCS)

Problem statement

Given a Binary Tree of 'N' nodes, find and print a list containing the sums of all the diagonals. When calculating the sum of each diagonal, consider the diagonals from right to left.

Diagonals of the binary tree are :

alt-text

There are three diagonals :

Diagonal 1 : 8 10 14

Diagonal 2 : 3 6 7 13

Diagonal 3 : 1 4

Example :

For the given binary tree : 

alt-text

Output : 24 14 2

Explanation: Rightmost diagonal contains elements {5, 10 , 9} its sum is 24, middle diagonal contains elements {6, 3, 5} its sum is 14, leftmost diagonal contains elements {2}. Thus the answer should be “24 14 2”.
Detailed explanation ( Input/output format, Notes, Images )
Constraints :
0 <= N <= 10^5
0 <= node.data <= 10^9

Time Limit : 1 sec
Sample Input 1 :
1 2 3 -1 4 4 -1 -1 5 6 -1 -1 -1 -1 -1
Sample Output 1:
4 15 6
Explanation For Sample Input 1 :
The input binary tree will be represented as 

alt-text

From the above representation, the rightmost diagonal contains elements {1, 3}, the middle diagonal contains elements {2, 4, 4, 5} and leftmost diagonal elements are {6}. Thus the list should contain “4 15 6”.
Sample Input 2:
1 2 3 -1 -1 -1 -1
Sample Output 2 :
4 2
Full screen
Console