Timer is Running

Max Level Sum in Binary Tree
Submissions: 13030   Accuracy:


  Difficulty: Easy   Marks: 2
Associated Course(s):   Amazon SDE Test Series

Given a Binary Tree having positive and negative nodes. Find the maximum sum of any level in the given Binary Tree.


Input :               4
                    /   \
                   2    -5
                  / \    /\
                -1   3 -2  6
Output: 6
Explanation :
Sum of all nodes of 0'th level is 4
Sum of all nodes of 1'th level is -3
Sum of all nodes of 2'th level is 6
Hence maximum sum is 6

Input :          1
               /   \
             2      3
           /  \      \
          4    5      8
                    /   \
                   6     7  
Output :  17

First line of input contains the number of test cases T. For each test case, there will be only a single line of input which is a string representing the tree as described below: 

  1. The values in the string are in the order of level order traversal of the tree where, numbers denotes node values, and a character “N” denotes NULL child.

  2. For example:
    For the above tree, the string will be: 1 2 3 N N 4 6 N 5 N N 7 N

The function should return max sum level in the tree.

Your Task: 
You don't need to read input or print anything. Your task is to complete the function maxLevelSum() which takes the root node of the Binary Tree as input and returns the maximum sum of any horizontal level in the given Binary Tree.

Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).

1 <=T<= 100
1 <=Number of nodes<= 104
-1000 <=Data of a node<= 1000

Example(To be used only for expected output):

4 2 -5 -1 3 -2 6
1 2 3 4 5 N 8 N N N N 6 7

See the problem description for explanation of both the test cases.

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Harshit Sidhwa
Author: Shubham Joshi 1

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.