Maximum Level Sum in a Binary Tree

Given a Binary Tree with integer nodes, determine the maximum level sum among all the levels in the tree. The sum for a level is defined as the sum of all node values present at that particular level.

Input:

The first line will contain 'T', the number of test cases.
The subsequent lines represent each test case and consist of the tree elements in level order traversal. Node values are space-separated, with -1 representing null nodes.
For example, for the given tree:
example
the input would be:
1
2 3
4 -1 5 6
-1 7 -1 -1 -1 -1
-1 -1

Output:

For each test case, return a single integer that represents the maximum sum of the levels in the binary tree.
Each output for the test cases should be printed on a separate line.

Example:

Explanation for a sample test case:
Consider the following level descriptions:
Level 1: 1
Level 2: 2, 3
Level 3: 4, -1, 5, 6
Level 4: -1, 7, -1, -1, -1, -1
Level 5: -1, -1
The root node starts at level 1, with subsequent nodes following in level order.
Non-null nodes are parents for the next level nodes as described above.
For this tree structure, the maximum level sum would be determined by calculating each level sum.
Input: 1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 1000, where N is the number of nodes.
  • -10^5 <= DATA <= 10^5, and DATA is not equal to -1.
  • Time limit: 1 second

Note:

You do not need to print anything. Implement the provided function to compute the result.

AnswerBot
4mo

Find the maximum level sum in a binary tree by calculating the sum of nodes at each level.

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

  • Keep track of the maximum...read more

Help your peers!
Select
Add answer anonymously...

HSBC Group Software Engineer Trainee interview questions & answers

A Software Engineer Trainee was asked 6mo agoQ. What concepts do you know in Cybersecurity?
A Software Engineer Trainee was asked 6mo agoQ. Write some SQL queries.
A Software Engineer Trainee was asked 6mo agoQ. Write React code for a login page.

Popular interview questions of Software Engineer Trainee

A Software Engineer Trainee was asked 6mo agoQ1. What concepts do you know in Cybersecurity?
A Software Engineer Trainee was asked 6mo agoQ2. Write some SQL queries.
A Software Engineer Trainee was asked 6mo agoQ3. Write React code for a login page.
HSBC Group Software Engineer Trainee 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