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++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
class Node {
public :
int data;
Node *left, *right;
Node( int data)
{
this ->data = data;
this ->left = NULL;
this ->right = NULL;
}
};
int findMax(Node* root)
{
if (root == NULL)
return INT_MIN;
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;
}
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);
cout << "Maximum element is " << findMax(root) << endl;
return 0;
}
|
C
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *left, *right;
};
struct Node* newNode( int data)
{
struct Node* node
= ( struct Node*) malloc ( sizeof ( struct Node));
node->data = data;
node->left = node->right = NULL;
return (node);
}
int findMax( struct Node* root)
{
if (root == NULL)
return INT_MIN;
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;
}
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);
printf ( "Maximum element is %d \n" , findMax(root));
return 0;
}
|
Java
class Node {
int data;
Node left, right;
public Node( int data)
{
this .data = data;
left = right = null ;
}
}
class BinaryTree {
Node root;
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;
}
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 );
System.out.println( "Maximum element is "
+ tree.findMax(tree.root));
}
}
|
Python3
class newNode:
def __init__( self , data):
self .data = data
self .left = self .right = None
def findMax(root):
if (root = = None ):
return float ( '-inf' )
res = root.data
lres = findMax(root.left)
rres = findMax(root.right)
if (lres > res):
res = lres
if (rres > res):
res = rres
return res
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 )
print ( "Maximum element is" ,
findMax(root))
|
C#
using System;
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;
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;
}
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);
Console.WriteLine( "Maximum element is "
+ BinaryTree.findMax(tree.root));
}
}
|
Javascript
<script>
let root;
class Node
{
constructor(data) {
this .left = null ;
this .right = null ;
this .data = data;
}
}
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);
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++
int findMin(Node *root)
{
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
int findMin( struct Node* root)
{
if (root == NULL)
return INT_MAX;
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
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
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
|
C#
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;
}
|
Javascript
<script>
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).
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 neveropen!