# Maximum sum of leaf nodes among all levels of the given binary tree

Given a Binary Tree having positive and negative nodes, the task is to find the maximum sum of leaf nodes among all level of the given binary tree.

**Examples:**

Input:4 / \ 2 -5 / \ -1 3Output:2 Sum of all leaves at 0^{th}level is 0. Sum of all leaves at 1^{st}level is -5. Sum of all leaves at 2^{nd}level is 2. Hence maximum sum is 2.Input:1 / \ 2 3 / \ \ 4 5 8 / \ 6 7Output:13

**Approach:** The idea to solve the above problem is to do level order traversal of tree. While doing traversal, process nodes of different level separately. For every level being processed, compute the sum of leaf nodes in the level and keep track of the maximum sum.

Below is the implementation of the above approach:

`// C++ implementation of the approach ` `#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 return the maximum sum of leaf nodes ` `// at any level in tree using level order traversal ` `int` `maxLeafNodesSum(` `struct` `Node* root) ` `{ ` ` ` ` ` `// Base case ` ` ` `if` `(root == NULL) ` ` ` `return` `0; ` ` ` ` ` `// Initialize result ` ` ` `int` `result = 0; ` ` ` ` ` `// Do Level order traversal keeping track ` ` ` `// of the 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 leaf node's value to current sum ` ` ` `if` `(temp->left == NULL && temp->right == NULL) ` ` ` ` ` `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 sum of leaf nodes 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); ` `} ` ` ` `// Drive 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); ` ` ` `cout << maxLeafNodesSum(root) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

13

## Recommended Posts:

- Maximum sum of non-leaf nodes among all levels of the given binary tree
- Print all nodes between two given levels in Binary Tree
- Print Levels of all nodes in a Binary Tree
- Sum of all leaf nodes of binary tree
- Print even positioned nodes of even levels in level order of the given binary tree
- Count Non-Leaf nodes in a Binary Tree
- Product of all leaf nodes of binary tree
- Print the nodes of binary tree as they become the leaf node
- Print Sum and Product of all Non-Leaf nodes in Binary Tree
- Print all leaf nodes of a binary tree from right to left
- Program to count leaf nodes in a binary tree
- Print all leaf nodes of a Binary Tree from left to right
- Pairwise Swap leaf nodes in a binary tree
- Print leaf nodes in binary tree from left to right using one stack
- Iterative program to count leaf nodes in a Binary Tree

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.