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
andDATA != -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.
Be the first one to answer
Add answer anonymously...
Top Housing.com Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in Housing.com Software Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app