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:
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.
Be the first one to answer
Add answer anonymously...
Top HSBC Group Software Engineer Trainee interview questions & answers
Popular interview questions of Software Engineer Trainee
Top HR questions asked in HSBC Group Software Engineer Trainee
>
HSBC Group Software Engineer Trainee Interview Questions
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