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
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Dijkstra's Algorithm to find Shortest Paths from a Source to all Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example
12 min read
Selection Sort Selection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire array is sorted.First we find the smallest element an
8 min read