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>
usingnamespacestd;
// A tree node
classNode {
public:
intdata;
Node *left, *right;
/* Constructor that allocates a new
node with the given data and NULL
left and right pointers. */
Node(intdata)
{
this->data = data;
this->left = NULL;
this->right = NULL;
}
};
// Returns maximum value in a given
// Binary Tree
intfindMax(Node* root)
{
// Base case
if(root == NULL)
returnINT_MIN;
// Return maximum of 3 values:
// 1) Root's data 2) Max in Left Subtree
// 3) Max in right subtree
intres = root->data;
intlres = findMax(root->left);
intrres = findMax(root->right);
if(lres > res)
res = lres;
if(rres > res)
res = rres;
returnres;
}
// Driver Code
intmain()
{
Node* NewRoot = NULL;
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
cout << "Maximum element is "<< findMax(root) << endl;
return0;
}
// 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
structNode {
intdata;
structNode *left, *right;
};
// A utility function to create a new node
structNode* newNode(intdata)
{
structNode* node
= (structNode*)malloc(sizeof(structNode));
node->data = data;
node->left = node->right = NULL;
return(node);
}
// Returns maximum value in a given Binary Tree
intfindMax(structNode* root)
{
// Base case
if(root == NULL)
returnINT_MIN;
// Return maximum of 3 values:
// 1) Root's data 2) Max in Left Subtree
// 3) Max in right subtree
intres = root->data;
intlres = findMax(root->left);
intrres = findMax(root->right);
if(lres > res)
res = lres;
if(rres > res)
res = rres;
returnres;
}
// Driver code
intmain(void)
{
structNode* NewRoot = NULL;
structNode* 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));
return0;
}
Java
// Java code to Find maximum (or minimum) in
// Binary Tree
// A binary tree node
classNode {
intdata;
Node left, right;
publicNode(intdata)
{
this.data = data;
left = right = null;
}
}
classBinaryTree {
Node root;
// Returns the max value in a binary tree
staticintfindMax(Node node)
{
if(node == null)
returnInteger.MIN_VALUE;
intres = node.data;
intlres = findMax(node.left);
intrres = findMax(node.right);
if(lres > res)
res = lres;
if(rres > res)
res = rres;
returnres;
}
/* Driver code */
publicstaticvoidmain(String args[])
{
BinaryTree tree = newBinaryTree();
tree.root = newNode(2);
tree.root.left = newNode(7);
tree.root.right = newNode(5);
tree.root.left.right = newNode(6);
tree.root.left.right.left = newNode(1);
tree.root.left.right.right = newNode(11);
tree.root.right.right = newNode(9);
tree.root.right.right.left = newNode(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
classnewNode:
def__init__(self, data):
self.data =data
self.left =self.right =None
# Returns maximum value in a
# given Binary Tree
deffindMax(root):
# Base case
if(root ==None):
returnfloat('-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
returnres
# 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
usingSystem;
// A binary tree node
publicclassNode {
publicintdata;
publicNode left, right;
publicNode(intdata)
{
this.data = data;
left = right = null;
}
}
publicclassBinaryTree {
publicNode root;
// Returns the max value in a binary tree
publicstaticintfindMax(Node node)
{
if(node == null) {
returnint.MinValue;
}
intres = node.data;
intlres = findMax(node.left);
intrres = findMax(node.right);
if(lres > res) {
res = lres;
}
if(rres > res) {
res = rres;
}
returnres;
}
/* Driver code */
publicstaticvoidMain(string[] args)
{
BinaryTree tree = newBinaryTree();
tree.root = newNode(2);
tree.root.left = newNode(7);
tree.root.right = newNode(5);
tree.root.left.right = newNode(6);
tree.root.left.right.left = newNode(1);
tree.root.left.right.right = newNode(11);
tree.root.right.right = newNode(9);
tree.root.right.right.left = newNode(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
functionfindMax(node)
{
if(node == null)
returnNumber.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;
returnres;
}
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
document.write("Maximum element is "
+ findMax(root));
</script>
Output
Maximum 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++
intfindMin(Node *root)
{
//code
if(root==NULL)
{
returnINT_MAX;
}
intres=root->data;
intleft=findMin(root->left);
intright=findMin(root->right);
if(left<res)
{
res=left;
}
if(right<res)
{
res=right;
}
returnres;
}
C
// Returns minimum value in a given Binary Tree
intfindMin(structNode* root)
{
// Base case
if(root == NULL)
returnINT_MAX;
// Return minimum of 3 values:
// 1) Root's data 2) Max in Left Subtree
// 3) Max in right subtree
intres = root->data;
intlres = findMin(root->left);
intrres = findMin(root->right);
if(lres < res)
res = lres;
if(rres < res)
res = rres;
returnres;
}
Java
// Returns the min value in a binary tree
staticintfindMin(Node node)
{
if(node == null)
returnInteger.MAX_VALUE;
intres = node.data;
intlres = findMin(node.left);
intrres = findMin(node.right);
if(lres < res)
res = lres;
if(rres < res)
res = rres;
returnres;
}
Python3
# Returns the min value in a binary tree
deffind_min_in_BT(root):
ifroot isNone:
returnfloat('inf')
res =root.data
lres =find_min_in_BT(root.leftChild)
rres =find_min_in_BT(root.rightChild)
iflres < res:
res =lres
ifrres < res:
res =rres
returnres
# This code is contributed by Subhajit Nandi
C#
// Returns the min value in a binary tree
publicstaticintfindMin(Node node)
{
if(node == null)
returnint.MaxValue;
intres = node.data;
intlres = findMin(node.left);
intrres = findMin(node.right);
if(lres < res)
res = lres;
if(rres < res)
res = rres;
returnres;
}
// This code is contributed by Code_Mech
Javascript
<script>
// Returns the min value in a binary tree
functionfindMin(node) {
if(node == null) return2147483647;
varres = node.data;
varlres = findMin(node.left);
varrres = findMin(node.right);
if(lres < res) res = lres;
if(rres < res) res = rres;
returnres;
}
</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).
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!