Given a Binary Tree having positive and negative nodes, the task is to find the maximum sum level in it.

**Examples:**

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 0'th level is 6 Hence maximum sum is 6 Input : 1 / \ 2 3 / \ \ 4 5 8 / \ 6 7 Output : 17

This problem is a variation of maximum width problem. The idea is to do level order traversal of tree. While doing traversal, process nodes of different level separately. For every level being processed, compute sum of nodes in the level and keep track of maximum sum.

Below is the implementation of the above idea:

`// A queue based C++ program to find maximum sum` `// of a level in Binary Tree` `#include <bits/stdc++.h>` `using` `namespace` `std;`
`/* A binary tree node has data, pointer to left child` ` ` `and a pointer to right child */`
`struct` `Node `
`{` ` ` `int` `data;`
` ` `struct` `Node *left, *right;`
`};` `// Function to find the maximum sum of a level in tree` `// using level order traversal` `int` `maxLevelSum(` `struct` `Node* root)`
`{` ` ` `// Base case`
` ` `if` `(root == NULL)`
` ` `return` `0;`
` ` `// Initialize result`
` ` `int` `result = root->data;`
` ` `// Do Level order traversal keeping track of number`
` ` `// of nodes at every level.`
` ` `queue<Node*> q;`
` ` `q.push(root);`
` ` `while` `(!q.empty())`
` ` `{`
` ` `// Get the size of queue when the level order`
` ` `// traversal for one level finishes`
` ` `int` `count = q.size();`
` ` `// Iterate for all the nodes in the queue currently`
` ` `int` `sum = 0;`
` ` `while` `(count--) `
` ` `{`
` ` `// Dequeue an node from queue`
` ` `Node* temp = q.front();`
` ` `q.pop();`
` ` `// Add this node's value to current sum.`
` ` `sum = sum + temp->data;`
` ` `// Enqueue left and right children of`
` ` `// dequeued node`
` ` `if` `(temp->left != NULL)`
` ` `q.push(temp->left);`
` ` `if` `(temp->right != NULL)`
` ` `q.push(temp->right);`
` ` `}`
` ` `// Update the maximum node count value`
` ` `result = max(sum, result);`
` ` `}`
` ` `return` `result;`
`}` `/* Helper function that allocates a new node with the` ` ` `given data and NULL left and right pointers. */`
`struct` `Node* newNode(` `int` `data)`
`{` ` ` `struct` `Node* node = ` `new` `Node;`
` ` `node->data = data;`
` ` `node->left = node->right = NULL;`
` ` `return` `(node);`
`}` `// Driver code` `int` `main()`
`{` ` ` `struct` `Node* root = newNode(1);`
` ` `root->left = newNode(2);`
` ` `root->right = newNode(3);`
` ` `root->left->left = newNode(4);`
` ` `root->left->right = newNode(5);`
` ` `root->right->right = newNode(8);`
` ` `root->right->right->left = newNode(6);`
` ` `root->right->right->right = newNode(7);`
` ` `/* Constructed Binary tree is:`
` ` `1`
` ` `/ \`
` ` `2 3`
` ` `/ \ \`
` ` `4 5 8`
` ` `/ \`
` ` `6 7 */`
` ` `cout << ` `"Maximum level sum is "` `<< maxLevelSum(root)`
` ` `<< endl;`
` ` `return` `0;`
`}` |

*chevron_right*

*filter_none*

**Output**

Maximum level sum is 17

__Complexity Analysis:__

**Time Complexity **: O(N) where N is the total number of nodes in the tree.

In level order traversal, every node of the tree is processed once and hence the complexity due to the level order traversal is O(N) if there are total N nodes in the tree. Also, while processing every node, we are maintaining the sum at each level, however, this does not affect the overall time complexity. Therefore, the time complexity is O(N).

**Auxiliary Space :** O(w) where w is the maximum width of the tree.

In level order traversal, a queue is maintained whose maximum size at any moment can go upto the maximum width of the binary tree.

This article is contributed by **Shashank Mishra ( Gullu )**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Difference between sums of odd level and even level nodes of a Binary Tree
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Print the nodes corresponding to the level value for each level of a Binary Tree
- Count nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree
- Check if max sum level of Binary tree divides tree into two equal sum halves
- Find Maximum Level Sum in Binary Tree using Recursion
- Maximum absolute difference between any two level sum in a Binary Tree
- Difference between sums of odd level and even level nodes in an N-ary Tree
- Find maximum level product in Binary Tree
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Find the maximum node at a given level in a binary tree
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Sum of all nodes at Kth level in a Binary Tree
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Maximum absolute difference between any two level sum in a N-ary Tree
- Maximum level sum in N-ary Tree
- Find if given vertical level of binary tree is sorted or not
- Find the numbers present at Kth level of a Fibonacci Binary Tree
- Find the Level of a Binary Tree with Width K