Find Maximum Level Sum in Binary Tree using Recursion
Last Updated :
15 Jul, 2021
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++
// 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;
}
Java
// Java implementation of the approach
class GFG
{
// A binary tree node has data, pointer to
// the left child and the right child
static class Node
{
int data;
Node left, right;
};
// Helper function that allocates a
// new node with the given data and
// null left and right pointers
static Node newNode(int data)
{
Node node = new Node();
node.data = data;
node.left = node.right = null;
return (node);
}
// Function to return the maximum
// levels in the given tree
static int maxLevel( Node root)
{
if (root == null)
return 0;
return (1 + Math.max(maxLevel(root.left),
maxLevel(root.right)));
}
// Function to find the maximum sum of a
// level in the tree using recursion
static void maxLevelSum(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
static int maxLevelSum( Node root)
{
// Maximum levels in the given tree
int max_level = maxLevel(root);
// To store the sum of every level
int sum[] = new int[max_level + 1];
// 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 = Math.max(maxSum, sum[i]);
// Return the maximum sum
return maxSum;
}
// Driver code
public static void main(String args[])
{
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 */
System.out.println(maxLevelSum(root));
}
}
// This code is contributed by Arnab Kundu
Python3
# Python3 implementation of above algorithm
# Utility class to create a node
class Node:
def __init__(self, key):
self.val = key
self.left = self.right = None
# Helper function that allocates a
# new node with the given data and
# None left and right pointers
def newNode(data):
node = Node(0)
node.data = data
node.left = node.right = None
return (node)
# Function to return the maximum
# levels in the given tree
def maxLevel( root):
if (root == None):
return 0
return (1 + max(maxLevel(root.left),
maxLevel(root.right)))
sum = []
# Function to find the maximum sum of a
# level in the tree using recursion
def maxLevelSum_( root, max_level , current):
global sum
# Base case
if (root == None):
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,
current + 1)
# Recursive call for the right child
maxLevelSum_(root.right, max_level,
current + 1)
# Function to find the maximum sum of a
# level in the tree using recursion
def maxLevelSum( root):
global sum
# Maximum levels in the given tree
max_level = maxLevel(root)
# To store the sum of every level
i = 0
sum = [None] * (max_level + 2)
while(i <= max_level + 1):
sum[i] = 0
i = i + 1
# Recursive function call to
# update the sum[] array
maxLevelSum_(root, max_level, 1)
# To store the maximum sum for a level
maxSum = 0
# For every level of the tree, update
# the maximum sum of a level so far
i = 1
while ( i <= max_level ):
maxSum = max(maxSum, sum[i])
i = i + 1
# Return the maximum sum
return maxSum
# Driver code
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
print( maxLevelSum(root))
# This code is contributed by Arnab Kundu
C#
// C# implementation of the approach
using System;
class GFG
{
// A binary tree node has data,
// pointer to the left child
// and the right child
public class Node
{
public int data;
public Node left, right;
};
// Helper function that allocates a
// new node with the given data and
// null left and right pointers
static Node newNode(int data)
{
Node node = new Node();
node.data = data;
node.left = node.right = null;
return (node);
}
// Function to return the maximum
// levels in the given tree
static int maxLevel( Node root)
{
if (root == null)
return 0;
return (1 + Math.Max(maxLevel(root.left),
maxLevel(root.right)));
}
// Function to find the maximum sum of a
// level in the tree using recursion
static void maxLevelSum(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
static int maxLevelSum( Node root)
{
// Maximum levels in the given tree
int max_level = maxLevel(root);
// To store the sum of every level
int []sum = new int[max_level + 1];
// 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 = Math.Max(maxSum, sum[i]);
// Return the maximum sum
return maxSum;
}
// Driver code
public static void Main(String []args)
{
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 */
Console.WriteLine(maxLevelSum(root));
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
// Javascript implementation of the approach
// A binary tree node has data, pointer to
// the left child and the right child
class Node
{
constructor()
{
this.data=0;
this.left=this.right=null;
}
}
// Helper function that allocates a
// new node with the given data and
// null left and right pointers
function newNode(data)
{
let node = new Node();
node.data = data;
node.left = node.right = null;
return (node);
}
// Function to return the maximum
// levels in the given tree
function maxLevel(root)
{
if (root == null)
return 0;
return (1 + Math.max(maxLevel(root.left),maxLevel(root.right)));
}
// Function to find the maximum sum of a
// level in the tree using recursion
function maxLevelSum(root,max_level,sum,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
function _maxLevelSum(root)
{
// Maximum levels in the given tree
let max_level = maxLevel(root);
// To store the sum of every level
let sum = new Array(max_level + 1);
for(let i=0;i<max_level+1;i++)
sum[i]=0;
// Recursive function call to
// update the sum[] array
maxLevelSum(root, max_level, sum, 1);
// To store the maximum sum for a level
let maxSum = 0;
// For every level of the tree, update
// the maximum sum of a level so far
for (let i = 1; i <= max_level; i++)
maxSum = Math.max(maxSum, sum[i]);
// Return the maximum sum
return maxSum;
}
// Driver code
let 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 */
document.write(_maxLevelSum(root));
// This code is contributed by avanitrachhadiya2155
</script>
Similar Reads
Find maximum level sum in Binary Tree 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 6Output: 6Explanation :Sum of all nodes of 0'th level is 4Sum of all nodes of 1'th level is -3Sum of all nodes of 0'th level is 6Hence maximum sum is 6
15+ min read
Construct a Binary Tree in Level Order using Recursion Given an array of integers, the task is to construct a binary tree in level order fashion using Recursion. Examples Given an array arr[] = {15, 10, 20, 8, 12, 16, 25} Approach: Idea is to keep track of the number of child nodes in the left sub-tree and right sub-tree and then take the decision on th
10 min read
Find maximum vertical sum in binary tree Given a binary tree, find the maximum vertical level sum in binary tree.Examples: Input : 3 / \ 4 6 / \ / \ -1 -2 5 10 \ 8 Output : 14Vertical level having nodes 6 and 8 has maximumvertical sum 14. Input : 1 / \ 5 8 / \ \ 2 -6 3 \ / -1 -4 \ 9Output : 4 A simple solution is to first find vertical lev
9 min read
Find the maximum sum leaf to root path in a Binary Tree Given a Binary Tree, the task is to find the maximum sum path from a leaf to a root.Example : Input: Output: 60Explanantion: There are three leaf to root paths 20->30->10, 5->30->10 and 15->10. The sums of these three paths are 60, 45 and 25 respectively. The maximum of them is 60 and
15+ min read
Find the sum of leafs at maximum level Given a binary tree containing n nodes. The task is to find the sum of all the leaf nodes present at maximum level.Examples: Input: 1 / \ 2 3 / \ / \ 4 5 6 7 / \ 8 9 Output: 17 Leaf nodes 8 and 9 are at maximum level. Their sum = (8 + 9) = 17. Input: 5 / \ 8 13 / 4 Output: 4 Approach: Perform iterat
9 min read