Maximum Level Sum Problem Statement

Given a binary tree with integer nodes, your task is to determine the maximum level sum among all the levels in the binary tree. The sum at any level is the sum of all nodes that are present at that level.

Input:

The first line contains an integer 'T', representing the number of test cases. The subsequent lines for each test case provide the tree elements in level order format, with node values separated by a single space. Use -1 to represent null nodes.

Output:

Return a single integer for each test case that indicates the maximum level sum of the binary tree. The output for each test case should be printed on a new line.

Example:

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

The binary tree is composed as follows:

  • Level 1: Node = 1
  • Level 2: Nodes = 2, 3
  • Level 3: Nodes = 4, (null), 5, 6
  • Level 4: Nodes = (null), 7, (null), (null), (null), (null)
  • Level 5: Nodes = (null), (null)

The sums of each level are: Level 1 = 1, Level 2 = 5, Level 3 = 15, Level 4 = 7. The maximum sum is 18 at Level 3.

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 1000, where N is the total number of nodes across all levels.
  • -10^5 <= DATA <= 10^5 and DATA != -1
  • All nodes are non-null availing the data constant.
  • Time limit per test case is 1 second.
Note:

The input format example was illustrative. The actual input is a flat sequence of values in a single line, considering null (-1) for understanding parent-child assignments implicitly.

AnswerBot
4mo

Find the maximum level sum in a binary tree with integer nodes.

  • Traverse the binary tree level by level and calculate the sum of nodes at each level.

  • Keep track of the maximum level sum encountered so f...read more

Help your peers!
Select
Add answer anonymously...

Top Software Developer Interview Questions Asked at Housing.com

Q. Given a rotated sorted array, how can you search for a target value efficiently ...read more
Q. Given a sorted array that has been rotated by some number of positions, find the...read more
Q. How would you add two integers that cannot be stored even in a long long int?
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