Find Maximum Level Sum in Binary Tree using Recursion
Given a Binary Tree having positive and negative nodes, the task is to find the maximum sum level in it and print the maximum sum.
Examples:
Input:
4
/ \
2 -5
/ \ / \
-1 3 -2 6
Output: 6
Sum of all nodes of the 1st level is 4.
Sum of all nodes of the 2nd level is -3.
Sum of all nodes of the 3rd level is 6.
Hence, the maximum sum is 6.
Input:
1
/ \
2 3
/ \ \
4 5 8
/ \
6 7
Output: 17
Approach: Find the maximum level in the given binary tree then create an array sum[] where sum[i] will store the sum of the elements at level i.
Now, write a recursive function that takes a node of the tree and its level as the argument and updates the sum for the current level then makes recursive calls for the children with the updated level as one more than the current level (this is because children are at a level one more than their parent). Finally, print the maximum value from the sum[] array.
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 // the left child and the right child struct Node { int data; struct Node *left, *right; }; // 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); } // Function to return the maximum // levels in the given tree int maxLevel(struct Node* root) { if (root == NULL) return 0; return (1 + max(maxLevel(root->left), maxLevel(root->right))); } // Function to find the maximum sum of a // level in the tree using recursion void maxLevelSum(struct Node* root, int max_level, int sum[], int current) { // Base case if (root == NULL) return; // Add current node's data to // its level's sum sum[current] += root->data; // Recursive call for the left child maxLevelSum(root->left, max_level, sum, current + 1); // Recursive call for the right child maxLevelSum(root->right, max_level, sum, current + 1); } // Function to find the maximum sum of a // level in the tree using recursion int maxLevelSum(struct Node* root) { // Maximum levels in the given tree int max_level = maxLevel(root); // To store the sum of every level int sum[max_level + 1] = { 0 }; // Recursive function call to // update the sum[] array maxLevelSum(root, max_level, sum, 1); // To store the maximum sum for a level int maxSum = 0; // For every level of the tree, update // the maximum sum of a level so far for (int i = 1; i <= max_level; i++) maxSum = max(maxSum, sum[i]); // Return the maximum sum return maxSum; } // 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 << maxLevelSum(root); return 0; } |
17
Recommended Posts:
- Find maximum level sum in Binary Tree
- Find maximum level product in Binary Tree
- Find the maximum node at a given level in a binary tree
- Find the node with maximum value in a Binary Search Tree using recursion
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Find maximum and minimum element in binary tree without using recursion or stack or queue
- Find if given vertical level of binary tree is sorted or not
- Find the node with minimum value in a Binary Search Tree using recursion
- Difference between sums of odd level and even level nodes of a Binary Tree
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Find maximum vertical sum in binary tree
- Find maximum (or minimum) in Binary Tree
- Find maximum among all right nodes in Binary Tree
- Find the node with maximum value in a Binary Search Tree
- Find the maximum path sum between two leaves of 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.



