Find maximum (or minimum) in Binary Tree
Last Updated :
27 Feb, 2023
Given a Binary Tree, find the maximum(or minimum) element in it. For example, maximum in the following Binary Tree is 9.

In Binary Search Tree, we can find maximum by traversing right pointers until we reach the rightmost node. But in Binary Tree, we must visit every node to figure out maximum. So the idea is to traverse the given tree and for every node return maximum of 3 values.Â
- Node's data.
- Maximum in node's left subtree.
- Maximum in node's right subtree.
Below is the implementation of above approach.Â
C++
// C++ program to find 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;
}
};
// 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;
}
// Driver Code
int main()
{
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);
// Function call
cout << "Maximum element is " << findMax(root) << endl;
return 0;
}
// This code is contributed by
// rathbhupendra
C
// C program to find 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);
}
// 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;
}
// Driver code
int main(void)
{
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);
// Function call
printf("Maximum element is %d \n", findMax(root));
return 0;
}
Java
// Java code to Find maximum (or minimum) in
// Binary Tree
// A binary tree node
class Node {
int data;
Node left, right;
public Node(int data)
{
this.data = data;
left = right = null;
}
}
class BinaryTree {
Node root;
// Returns the max value in a binary tree
static int findMax(Node node)
{
if (node == null)
return Integer.MIN_VALUE;
int res = node.data;
int lres = findMax(node.left);
int rres = findMax(node.right);
if (lres > res)
res = lres;
if (rres > res)
res = rres;
return res;
}
/* Driver code */
public static void main(String args[])
{
BinaryTree tree = new BinaryTree();
tree.root = new Node(2);
tree.root.left = new Node(7);
tree.root.right = new Node(5);
tree.root.left.right = new Node(6);
tree.root.left.right.left = new Node(1);
tree.root.left.right.right = new Node(11);
tree.root.right.right = new Node(9);
tree.root.right.right.left = new Node(4);
// Function call
System.out.println("Maximum element is "
+ tree.findMax(tree.root));
}
}
// This code is contributed by Kamal Rawal
Python3
# Python3 program to find maximum
# and minimum in a Binary Tree
# A class to create a new node
class newNode:
def __init__(self, data):
self.data = data
self.left = self.right = None
# Returns maximum value in a
# given Binary Tree
def findMax(root):
# Base case
if (root == None):
return float('-inf')
# 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
# Driver Code
if __name__ == '__main__':
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)
# Function call
print("Maximum element is",
findMax(root))
# This code is contributed by PranchalK
C#
// C# code to Find maximum (or minimum) in
// Binary Tree
using System;
// A binary tree node
public class Node {
public int data;
public Node left, right;
public Node(int data)
{
this.data = data;
left = right = null;
}
}
public class BinaryTree {
public Node root;
// Returns the max value in a binary tree
public static int findMax(Node node)
{
if (node == null) {
return int.MinValue;
}
int res = node.data;
int lres = findMax(node.left);
int rres = findMax(node.right);
if (lres > res) {
res = lres;
}
if (rres > res) {
res = rres;
}
return res;
}
/* Driver code */
public static void Main(string[] args)
{
BinaryTree tree = new BinaryTree();
tree.root = new Node(2);
tree.root.left = new Node(7);
tree.root.right = new Node(5);
tree.root.left.right = new Node(6);
tree.root.left.right.left = new Node(1);
tree.root.left.right.right = new Node(11);
tree.root.right.right = new Node(9);
tree.root.right.right.left = new Node(4);
// Function call
Console.WriteLine("Maximum element is "
+ BinaryTree.findMax(tree.root));
}
}
// This code is contributed by Shrikant13
JavaScript
<script>
// Javascript code to Find maximum (or minimum)
// in Binary Tree
let root;
class Node
{
constructor(data) {
this.left = null;
this.right = null;
this.data = data;
}
}
// Returns the max value in a binary tree
function findMax(node)
{
if (node == null)
return Number.MIN_VALUE;
let res = node.data;
let lres = findMax(node.left);
let rres = findMax(node.right);
if (lres > res)
res = lres;
if (rres > res)
res = rres;
return res;
}
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);
// Function call
document.write("Maximum element is "
+ findMax(root));
</script>
OutputMaximum element is 11
Time Complexity: O(N), where N is number of nodes as every node of tree is traversed once by findMax() and findMin().
Auxiliary Space: O(N) , Recursive call for each node tree considered as stack space.
 Similarly, we can find the minimum element in a Binary tree by comparing three values. Below is the function to find a minimum in Binary Tree.Â
C++
int findMin(Node *root)
{
//code
if(root==NULL)
{
return INT_MAX;
}
int res=root->data;
int left=findMin(root->left);
int right=findMin(root->right);
if(left<res)
{
res=left;
}
if(right<res)
{
res=right;
}
return res;
}
C
// Returns 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;
}
Java
// Returns the min value in a binary tree
static int findMin(Node node)
{
if (node == null)
return Integer.MAX_VALUE;
int res = node.data;
int lres = findMin(node.left);
int rres = findMin(node.right);
if (lres < res)
res = lres;
if (rres < res)
res = rres;
return res;
}
Python3
# Returns the min value in a binary tree
def find_min_in_BT(root):
if root is None:
return float('inf')
res = root.data
lres = find_min_in_BT(root.leftChild)
rres = find_min_in_BT(root.rightChild)
if lres < res:
res = lres
if rres < res:
res = rres
return res
# This code is contributed by Subhajit Nandi
C#
// Returns the min value in a binary tree
public static int findMin(Node node)
{
if (node == null)
return int.MaxValue;
int res = node.data;
int lres = findMin(node.left);
int rres = findMin(node.right);
if (lres < res)
res = lres;
if (rres < res)
res = rres;
return res;
}
// This code is contributed by Code_Mech
JavaScript
<script>
// Returns the min value in a binary tree
function findMin(node) {
if (node == null) return 2147483647;
var res = node.data;
var lres = findMin(node.left);
var rres = findMin(node.right);
if (lres < res) res = lres;
if (rres < res) res = rres;
return res;
}
</script>
Complexity Analysis:
Time Complexity: O(N).
In the recursive function calls, every node of the tree is processed once and hence the complexity due to the function is O(N) if there are total N nodes in the tree. Therefore, the time complexity is O(N).
Space Complexity: O(N).
Recursive call is happening. The every node is processed once and considering the stack space, the space complexity will be O(N).
Similar Reads
Print Binary Search Tree in Min Max Fashion Given a Binary Search Tree (BST), the task is to print the BST in min-max fashion. What is min-max fashion? A min-max fashion means you have to print the maximum node first then the minimum then the second maximum then the second minimum and so on. Examples: Input: 100 / \ 20 500 / \ 10 30 \ 40 Outp
10 min read
Find Minimum Depth of a Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. For example, minimum depth of below Binary Tree is 2. Note that the path must end on a leaf node. For example, the minimum depth of below Bi
15 min read
Sum and Product of maximum and minimum element in Binary Tree 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
12 min read
Finding Minimum Steps in Special Binary Tree Given two integer values (i and j) as input representing two nodes of a special binary tree. The special binary tree has this property that the root node's value is always 1 and for every node, its left child will be the node's value * 3, and the right child will be ( node's value * 3) + 1. The give
8 min read
Minimum in a Binary Search Tree Given the root of a Binary Search Tree. The task is to find the minimum valued element in this given BST.Example: Input: Output: 1Explanation: The minimum element in the given BST is 1.Input: Output: 2Explanation: The minimum element in the given BST is 2Table of Content[Naive Approach] Using Inorde
12 min read
Find minimum in K Dimensional Tree We strongly recommend to refer below post as a prerequisite of this. K Dimensional Tree | Set 1 (Search and Insert) In this post find minimum is discussed. The operation is to find minimum in the given dimension. This is especially needed in delete operation. For example, consider below KD Tree, if
13 min read
Find LCA in Binary Tree using RMQ The article describes an approach to solving the problem of finding the LCA of two nodes in a tree by reducing it to an RMQ problem. The Lowest Common Ancestor (LCA) of two nodes u and v in a rooted tree T is defined as the node located farthest from the root that has both u and v as descendants.For
15+ min read
Minimum nodes for same right view in Binary tree Given a binary tree where each node contains a positive integer, the task is to find the minimum number of nodes that need to be removed from the tree, such that the resulting tree has the same right view as the original tree.Examples:Input: Output: 2Explanation: The right view of the given binary t
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
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