Deepest Leaves Sum Problem Statement

Given a binary tree of integers, your task is to calculate the sum of all the leaf nodes present at the deepest level of this binary tree. If there are no such nodes, output 0.

Input:

The first line contains an integer 'T' denoting the number of test cases. 
Each test case includes one line of elements in level order form, with node values separated by a single space. A null node is represented by -1.

Output:

For each test case, output the sum of all leaf nodes at the deepest level, printed on a new line.

Example:

Input:
1 
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output:
7

Explanation:

The binary tree represented by the input has depth levels as depicted in a given image, with the deepest leaves being [7], thus the sum is 7.

Constraints:

  • 1 ≤ T ≤ 102
  • 0 ≤ N ≤ 3000
  • The sum is guaranteed to fit within a 32-bit integer.
  • Time limit: 1 second.

Note:

You do not need to handle printing the output in your submission. Focus on implementing the function to solve the problem.

AnswerBot
4mo

Calculate the sum of leaf nodes at the deepest level of a binary tree.

  • Traverse the binary tree to find the deepest level of leaf nodes.

  • Sum the leaf nodes at the deepest level.

  • Handle null nodes represe...read more

Help your peers!
Select
Add answer anonymously...

Top Software Developer Interview Questions Asked at Walmart

Q. Tell me about yourself.
Q. How many languages do you know?
Q. What are your strengths?
Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits