Sum and Product of maximum and minimum element in Binary Tree
Last Updated :
23 Feb, 2023
Given a Binary Tree. The task is to find the sum and product of the maximum and minimum elements in it.
For example, sum of the maximum and minimum elements in the following binary tree is 10 and the product is 9.

The idea is to traverse the tree and find the maximum and minimum elements in the tree and print their product and sum.
To find the maximum element in the Binary Tree, recursively traverse the tree and return the maximum of three below:
- Current Node’s data.
- Maximum in node’s left subtree.
- Maximum in node’s right subtree.
Similarly, we can find the minimum element in the binary tree by comparing three values.
Implementation:
C++
// CPP program to find sum and product of
// maximum and minimum in a Binary Tree
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
// A tree node
class Node
{
public:
int data;
Node *left, *right;
/* Constructor that allocates
a new node with the given data
and NULL left and right pointers. */
Node(int data)
{
this->data = data;
this->left = NULL;
this->right = NULL;
}
};
// Function to return minimum value
// in a given Binary Tree
int findMin(Node* root)
{
// Base case
if (root == NULL)
return INT_MAX;
// Return minimum of 3 values:
// 1) Root's data 2) Max in Left Subtree
// 3) Max in right subtree
int res = root->data;
int lres = findMin(root->left);
int rres = findMin(root->right);
if (lres < res)
res = lres;
if (rres < res)
res = rres;
return res;
}
// Function to returns maximum value
// in a given Binary Tree
int findMax(Node* root)
{
// Base case
if (root == NULL)
return INT_MIN;
// Return maximum of 3 values:
// 1) Root's data 2) Max in Left Subtree
// 3) Max in right subtree
int res = root->data;
int lres = findMax(root->left);
int rres = findMax(root->right);
if (lres > res)
res = lres;
if (rres > res)
res = rres;
return res;
}
// Function to find sum of max and min
// elements in the Binary Tree
int findSum(int max , int min)
{
return max + min;
}
// Function to find product of max and min
// elements in the Binary Tree
int findProduct(int max, int min)
{
return max*min;
}
// Driver Code
int main()
{
// Create Binary Tree
Node* NewRoot = NULL;
Node* root = new Node(2);
root->left = new Node(7);
root->right = new Node(5);
root->left->right = new Node(6);
root->left->right->left = new Node(1);
root->left->right->right = new Node(11);
root->right->right = new Node(9);
root->right->right->left = new Node(4);
int max = findMax(root);
int min = findMin(root);
cout << "Sum of Maximum and Minimum element is " <<
findSum(max,min);
cout << "\nProduct of Maximum and Minimum element is " <<
findProduct(max,min);
return 0;
}
// This code is contributed by rathbhupendra
C
// C program to find sum and product of
// maximum and minimum in a Binary Tree
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
// A tree node
struct Node {
int data;
struct Node *left, *right;
};
// A utility function to create a new node
struct Node* newNode(int data)
{
struct Node* node = (struct Node*)
malloc(sizeof(struct Node));
node->data = data;
node->left = node->right = NULL;
return (node);
}
// Function to return minimum value
// in a given Binary Tree
int findMin(struct Node* root)
{
// Base case
if (root == NULL)
return INT_MAX;
// Return minimum of 3 values:
// 1) Root's data 2) Max in Left Subtree
// 3) Max in right subtree
int res = root->data;
int lres = findMin(root->left);
int rres = findMin(root->right);
if (lres < res)
res = lres;
if (rres < res)
res = rres;
return res;
}
// Function to returns maximum value
// in a given Binary Tree
int findMax(struct Node* root)
{
// Base case
if (root == NULL)
return INT_MIN;
// Return maximum of 3 values:
// 1) Root's data 2) Max in Left Subtree
// 3) Max in right subtree
int res = root->data;
int lres = findMax(root->left);
int rres = findMax(root->right);
if (lres > res)
res = lres;
if (rres > res)
res = rres;
return res;
}
// Function to find sum of max and min
// elements in the Binary Tree
int findSum(int max , int min)
{
return max + min;
}
// Function to find product of max and min
// elements in the Binary Tree
int findProduct(int max, int min)
{
return max*min;
}
// Driver Code
int main(void)
{
// Create Binary Tree
struct Node* NewRoot = NULL;
struct Node* root = newNode(2);
root->left = newNode(7);
root->right = newNode(5);
root->left->right = newNode(6);
root->left->right->left = newNode(1);
root->left->right->right = newNode(11);
root->right->right = newNode(9);
root->right->right->left = newNode(4);
int max = findMax(root);
int min = findMin(root);
printf("Sum of Maximum and Minimum element is %d",
findSum(max,min));
printf("\nProduct of Maximum and Minimum element is %d",
findProduct(max,min));
return 0;
}
Java
// JAVA program to find sum and product of
// maximum and minimum in a Binary Tree
import java.util.*;
class GFG
{
// A tree node
static class Node
{
public int data;
Node left, right;
/* Constructor that allocates
a new node with the given data
and null left and right pointers. */
Node(int data)
{
this.data = data;
this.left = null;
this.right = null;
}
};
// Function to return minimum value
// in a given Binary Tree
static int findMin(Node root)
{
// Base case
if (root == null)
return Integer.MAX_VALUE;
// Return minimum of 3 values:
// 1) Root's data 2) Max in Left Subtree
// 3) Max in right subtree
int res = root.data;
int lres = findMin(root.left);
int rres = findMin(root.right);
if (lres < res)
res = lres;
if (rres < res)
res = rres;
return res;
}
// Function to returns maximum value
// in a given Binary Tree
static int findMax(Node root)
{
// Base case
if (root == null)
return Integer.MIN_VALUE;
// Return maximum of 3 values:
// 1) Root's data 2) Max in Left Subtree
// 3) Max in right subtree
int res = root.data;
int lres = findMax(root.left);
int rres = findMax(root.right);
if (lres > res)
res = lres;
if (rres > res)
res = rres;
return res;
}
// Function to find sum of max and min
// elements in the Binary Tree
static int findSum(int max , int min)
{
return max + min;
}
// Function to find product of max and min
// elements in the Binary Tree
static int findProduct(int max, int min)
{
return max * min;
}
// Driver Code
public static void main(String[] args)
{
// Create Binary Tree
Node root = new Node(2);
root.left = new Node(7);
root.right = new Node(5);
root.left.right = new Node(6);
root.left.right.left = new Node(1);
root.left.right.right = new Node(11);
root.right.right = new Node(9);
root.right.right.left = new Node(4);
int max = findMax(root);
int min = findMin(root);
System.out.print("Sum of Maximum and Minimum element is " +
findSum(max, min));
System.out.print("\nProduct of Maximum and Minimum element is " +
findProduct(max, min));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python program to find sum and product of
# maximum and minimum in a Binary Tree
_MIN=-2147483648
_MAX=2147483648
# Helper function that allocates a new
# node with the given data and None left
# and right pointers.
class newNode:
# Constructor to create a new node
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# Function to return minimum value
# in a given Binary Tree
def findMin(root):
# Base case
if (root == None):
return _MAX
# Return minimum of 3 values:
# 1) Root's data 2) Max in Left Subtree
# 3) Max in right subtree
res = root.data
lres = findMin(root.left)
rres = findMin(root.right)
if (lres < res):
res = lres
if (rres < res):
res = rres
return res
# Function to returns maximum value
# in a given Binary Tree
def findMax( root):
# Base case
if (root == None):
return _MIN
""" Return maximum of 3 values:
1) Root's data 2) Max in Left Subtree
3) Max in right subtree"""
res = root.data
lres = findMax(root.left)
rres = findMax(root.right)
if (lres > res):
res = lres
if (rres > res):
res = rres
return res
# Function to find sum of max and min
# elements in the Binary Tree
def findSum( max , min):
return max + min
# Function to find product of max and min
# elements in the Binary Tree
def findProduct( max, min):
return max*min
# Driver Code
if __name__ == '__main__':
""" Create binary Tree """
root = newNode(2)
root.left = newNode(7)
root.right = newNode(5)
root.left.right = newNode(6)
root.left.right.left = newNode(1)
root.left.right.right = newNode(11)
root.right.right = newNode(9)
root.right.right.left = newNode(4)
max = findMax(root);
min = findMin(root);
print("Sum of Maximum and " +
"Minimum element is ",
findSum(max,min))
print("Product of Maximum and" +
"Minimum element is",
findProduct(max,min))
# This code is contributed
# Shubham Singh(SHUBHAMSINGH10)
C#
// C# program to find sum and product of
// maximum and minimum in a Binary Tree
using System;
class GFG
{
// A tree node
class Node
{
public int data;
public Node left, right;
/* Constructor that allocates
a new node with the given data
and null left and right pointers. */
public Node(int data)
{
this.data = data;
this.left = null;
this.right = null;
}
};
// Function to return minimum value
// in a given Binary Tree
static int findMin(Node root)
{
// Base case
if (root == null)
return int.MaxValue;
// Return minimum of 3 values:
// 1) Root's data 2) Max in Left Subtree
// 3) Max in right subtree
int res = root.data;
int lres = findMin(root.left);
int rres = findMin(root.right);
if (lres < res)
res = lres;
if (rres < res)
res = rres;
return res;
}
// Function to returns maximum value
// in a given Binary Tree
static int findMax(Node root)
{
// Base case
if (root == null)
return int.MinValue;
// Return maximum of 3 values:
// 1) Root's data 2) Max in Left Subtree
// 3) Max in right subtree
int res = root.data;
int lres = findMax(root.left);
int rres = findMax(root.right);
if (lres > res)
res = lres;
if (rres > res)
res = rres;
return res;
}
// Function to find sum of max and min
// elements in the Binary Tree
static int findSum(int max , int min)
{
return max + min;
}
// Function to find product of max and min
// elements in the Binary Tree
static int findProduct(int max, int min)
{
return max * min;
}
// Driver Code
public static void Main(String[] args)
{
// Create Binary Tree
Node root = new Node(2);
root.left = new Node(7);
root.right = new Node(5);
root.left.right = new Node(6);
root.left.right.left = new Node(1);
root.left.right.right = new Node(11);
root.right.right = new Node(9);
root.right.right.left = new Node(4);
int max = findMax(root);
int min = findMin(root);
Console.Write("Sum of Maximum and " +
"Minimum element is " +
findSum(max, min));
Console.Write("\nProduct of Maximum and " +
"Minimum element is " +
findProduct(max, min));
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
// javascript program to find sum and product of
// maximum and minimum in a Binary Tree
// A tree node
class Node {
constructor(val) {
this.data = val;
this.left = null;
this.right = null;
}
}
/*
* Constructor that allocates a new node with the given data and null left and
* right pointers.
*/
// Function to return minimum value
// in a given Binary Tree
function findMin(root) {
// Base case
if (root == null)
return Number.MAX_VALUE;
// Return minimum of 3 values:
// 1) Root's data 2) Max in Left Subtree
// 3) Max in right subtree
var res = root.data;
var lres = findMin(root.left);
var rres = findMin(root.right);
if (lres < res)
res = lres;
if (rres < res)
res = rres;
return res;
}
// Function to returns maximum value
// in a given Binary Tree
function findMax(root) {
// Base case
if (root == null)
return Number.MIN_VALUE;
// Return maximum of 3 values:
// 1) Root's data 2) Max in Left Subtree
// 3) Max in right subtree
var res = root.data;
var lres = findMax(root.left);
var rres = findMax(root.right);
if (lres > res)
res = lres;
if (rres > res)
res = rres;
return res;
}
// Function to find sum of max and min
// elements in the Binary Tree
function findSum(max , min) {
return max + min;
}
// Function to find product of max and min
// elements in the Binary Tree
function findProduct(max , min) {
return max * min;
}
// Driver Code
// Create Binary Tree
var root = new Node(2);
root.left = new Node(7);
root.right = new Node(5);
root.left.right = new Node(6);
root.left.right.left = new Node(1);
root.left.right.right = new Node(11);
root.right.right = new Node(9);
root.right.right.left = new Node(4);
var max = findMax(root);
var min = findMin(root);
document.write("Sum of Maximum and Minimum element is "
+ findSum(max, min));
document.write("<br/>Product of Maximum and Minimum element is "
+ findProduct(max, min));
// This code contributed by Rajput-Ji
</script>
OutputSum of Maximum and Minimum element is 12
Product of Maximum and Minimum element is 11
Time Comlexity :-O(N)
To find the maximum and minimum elements in a binary tree, we can perform a simple depth-first search traversal of the tree, keeping track of the current maximum and minimum values as we visit each node. The time complexity of this traversal is O(n), where n is the number of nodes in the tree, since we need to visit each node exactly once.
Space Complexity:- O(h)The space complexity for finding the maximum and minimum elements in a binary tree and calculating their sum and product can be expressed as O(h), where h is the height of the tree. This is because the space used by the algorithm is proportional to the maximum number of nodes that are simultaneously on the call stack during the recursive traversal of the tree
Similar Reads
Maximum product of any path in given Binary Tree
Given a binary tree of N nodes, the task is to find the maximum product of the elements of any path in the binary tree. Note: A path starts from the root and ends at any leaf in the tree. Examples: Input: 4 / \ 2 8 / \ / \2 1 3 4 Output: 128Explanation: Path in the given tree goes like {4, 8, 4} whi
5 min read
Find maximum (or minimum) in Binary Tree
Given a Binary Tree, find the maximum(or minimum) element in it. For example, maximum in the following Binary Tree is 9. Recommended PracticeMax and min element in Binary TreeTry It! In Binary Search Tree, we can find maximum by traversing right pointers until we reach the rightmost node. But in Bin
8 min read
Min-Max Product Tree of a given Binary Tree
Given a Binary Tree, the task is to convert the given Binary tree into Min-Max Product Tree and print the level-order sequence of the modified tree. Min-Max Product Tree: A Min-Max Product Tree contains the product of the minimum and maximum values of its left and right subtrees at each node. Note:
14 min read
Sum of nodes at maximum depth of a Binary Tree
Given a root node to a tree, find the sum of all the leaf nodes which are at maximum depth from root node. Example: 1 / \ 2 3 / \ / \ 4 5 6 7 Input : root(of above tree) Output : 22 Explanation: Nodes at maximum depth are: 4, 5, 6, 7. So, sum of these nodes = 22 While traversing the nodes compare th
15+ min read
Find maximum and minimum element in binary tree without using recursion or stack or queue
Given a binary tree. The task is to find out the maximum and minimum element in a binary tree without using recursion or stack or queue i.e, space complexity should be O(1). Examples: Input : 12 / \ 13 10 / \ 14 15 / \ / \ 21 24 22 23 Output : Max element : 24 Min element : 10 Input : 12 / \ 19 82 /
10 min read
Find the maximum element of every subtree of a Binary Tree
Given a Binary Tree, find the maximum element of every subtree of it. Examples :Input : 1 / \ 2 3 / \ / \ 4 5 6 7 Output : [4, 5, 5, 7, 6, 7, 7] Explanation: The maximum element of the subtree rooted at node 4 is 4. The maximum element of the subtree rooted at node 2 is 5. The maximum element of the
15+ min read
Maximum Path Sum in a Binary Tree
Given a binary tree, the task is to find the maximum path sum. The path may start and end at any node in the tree.Example: Input: Output: 42Explanation: Max path sum is represented using green colour nodes in the above binary tree.Input: Output: 31Explanation: Max path sum is represented using green
8 min read
Maximum parent children sum in Binary tree
Given a Binary Tree, find the maximum sum in a binary tree by adding the parent with its children. Exactly three Node needs to be added. If the tree does not have a node with both of its children as not NULL, return 0. We simply traverse the tree and find the Node that has the maximum sum. We need t
5 min read
Print Sum and Product of all Non-Leaf nodes in Binary Tree
Given a Binary tree. The task is to find and print the product and sum of all internal nodes (non-leaf nodes) in the tree. In the above tree, only two nodes 1 and 2 are non-leaf nodes. Therefore, product of non-leaf nodes = 1 * 2 = 2. And sum of non-leaf nodes = 1 + 2 =3. Examples: Input : 1 / \ 2 3
8 min read
Find maximum product of K integers in a Binary Search Tree
Given a Binary search tree(BST) and a positive integer K, find the maximum product of k integers in the tree. Return the product as an integer. Examples: Input: K = 3, 10 / \ 3 12 / \ / \ 2 4 11 13Output: 1716Explanation: The maximum product of 3 integers can be obtained by selecting 12, 13, and 11.
10 min read