Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5725 |

@blackshadows | 5685 |

@akhayrutdinov | 5111 |

@mb1973 | 4989 |

@Quandray | 4908 |

@saiujwal13083 | 4156 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3511 |

@sushant_a | 3453 |

@KshamaGupta | 3318 |

@the_coder95 | 3302 |

Complete Leaderboard | |

Handle | Score |

@avaiesshaik782 | 662 |

@arpitsatnalika | 629 |

@code_it_ | 591 |

@prakharxyz | 583 |

@tujhekya | 542 |

@Rahul Jadhav 1 | 458 |

@sivanandh03 | 448 |

@satyam0460 | 433 |

@imnm | 426 |

@iamronak | 424 |

@18eumc107 | 421 |

Complete Leaderboard |

Consider Red lines of slope -1 passing between nodes (in following diagram). The diagonal sum in a binary tree is the sum of all node’s data 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.

**Example 1:**

Input:4 / \ 1 3 / 3Output:7 4

**Example 2:**

Input:10 / \ 8 2 / \ / 3 5 2Output:12 15 3

**Your Task:**

You don't need to take input. Just complete the function** diagonalSum() **that takes root **node** of the tree as parameter and returns an array containing the diagonal sums for every diagonal present in the tree with slope -1.

**Expected Time Complexity**: O(N).

**Expected Auxiliary Space: **O(N).

**Constraints:**

1<=Number of nodes<=10^{5}

**Note: **The **Input/Ouput** format and **Example** given are used for system's internal purpose, and should be used by a user for **Expected Output** only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Diagonal Sum In Binary Tree

...

Please choose 'ReadOnlyMode' if you needn't to 'Edit' the problem e.g. viewing OJ's solution, TestCase Files (TCFs), TimeLimit etc.