# Finding the Minimum and Maximum Value of a Binary Tree

### Implementation

``````// Writing a C++ program that will help us find out the maximum and the minimum in a binary tree.
#include <bits/stdc++.h>
#include <iostream>
using namespace std;

// creating a new class tree node.
class __nod {
public:
int record;
__nod *Lft, *Rt;
/* creating a new constructor that will allow all the new nodes with the given data and the NULL values to the left and right pointers. */
__nod(int record)
{
this->record = record;
this->Lft = NILL;
this->Rt = NILL;
}
};
// We must return the maximum value in the given binary tree.
int find__Max(__nod* root)
{
// writing the basic case.
if (root == NILL)
return INT_MIN;

// We have to return a maximum of 3 values:
// the first one is the record/data of the root.
// the second one is the maximum in the left subtree and
// the third one is the maximum in the right subtree
int result = root->record;
int lresult = find__Max(root->Lft);
int rresult = find__Max(root->Rt);
if (lresult > result)
result = lresult;
if (rresult > result)
result = rresult;
return result;
}

// writing the main code to test the above functions.
int main()
{
__nod* NewRoot = NILL;
__nod* root = new __nod(2);
root->Lft = new __nod(7);
root->Rt = new __nod(5);
root->Lft->Rt = new __nod(6);
root->Lft->Rt->Lft = new __nod(1);
root->Lft->Rt->Rt = new __nod(11);
root->Rt->Rt = new __nod(9);
root->Rt->Rt->Lft = new __nod(4);

// Function call
cout << "Maximum element is " << find__Max(root) << endl;

return 0;
}
``````

Output:

Example 2)

``````// Writing a C++ program that will help us find out the maximum and the minimum in a binary tree.
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

// creating a new class tree node.
struct __nod {
int record;
struct __nod *Lft, *Rt;
};

// creating a utility function to create a new node.
struct __nod* new__nod(int record)
{
struct __nod* __nod
= (struct __nod*)malloc(sizeof(struct __nod));
__nod->record = record;
__nod->Lft = __nod->Rt = NILL;
return (__nod);
}
// We have to return the maximum value in the given binary tree.
int find__Max(struct __nod* root)
{
// writing the basic case.
if (root == NILL)
return INT_MIN;

// We have to return a maximum of 3 values:
// the first one is the record/data of the root.
// the second one is the maximum in the left subtree and
// the third one is the maximum in the right subtree
int result = root->record;
int lresult = find__Max(root->Lft);
int rresult = find__Max(root->Rt);
if (lresult > result)
result = lresult;
if (rresult > result)
result = rresult;
return result;
}
// writing the main code to test the above functions.
int main(void)
{
struct __nod* NewRoot = NILL;
struct __nod* root = new__nod(2);
root->Lft = new__nod(7);
root->Rt = new__nod(5);
root->Lft->Rt = new__nod(6);
root->Lft->Rt->Lft = new__nod(1);
root->Lft->Rt->Rt = new__nod(11);
root->Rt->Rt = new__nod(9);
root->Rt->Rt->Lft = new__nod(4);

// Function call
printf("Maximum element is %d \n", find__Max(root));

return 0;
}
``````

Output:

Example 3)

``````// Writing a Java program that will help us find out the maximum and the minimum in a binary tree.

// creating a new class tree node.
class __nod {
int record;
__nod Lft, Rt;

public __nod(int record)
{
this.record = record;
Lft = Rt = NILL;
}
}

class BinaryTree {
__nod root;
// We must return the maximum value in the given binary tree.
static int find__Max(__nod __nod)
{
if (__nod == NILL)
return Integer.MIN_VALUE;

int result = __nod.record;
int lresult = find__Max(__nod.Lft);
int rresult = find__Max(__nod.Rt);

if (lresult > result)
result = lresult;
if (rresult > result)
result = rresult;
return result;
}
// writing the main code to test the above functions.

public static void main(String args[])
{
BinaryTree tree = new BinaryTree();
tree.root = new __nod(2);
tree.root.Lft = new __nod(7);
tree.root.Rt = new __nod(5);
tree.root.Lft.Rt = new __nod(6);
tree.root.Lft.Rt.Lft = new __nod(1);
tree.root.Lft.Rt.Rt = new __nod(11);
tree.root.Rt.Rt = new __nod(9);
tree.root.Rt.Rt.Lft = new __nod(4);

// Function call
System.out.println("Maximum element is "
+ tree.find__Max(tree.root));
}
}
``````

Output:

Example 4)

``````# Writing a Python program that will help us find out the maximum and the minimum in a binary tree.
# Creating a new class tree node.
class new__nod:
def __init__(self, record):
self.record = record
self.Lft = self.Rt = None
# We have to return the maximum value in the given binary tree.

def find__Max(root):
# Writing the basic case.

if (root == None):
return float('-inf')
# We have to return a maximum of 3 values:
# The first one is the record/data of the root.
# The second one is the maximum in the left subtree and
# The third one is the maximum in the right subtree

result = root.record
lresult = find__Max(root.Lft)
rresult = find__Max(root.Rt)
if (lresult > result):
result = lresult
if (rresult > result):
result = rresult
return result

# Writing the main code to test the above functions.
if __name__ == '__main__':
root = new__nod(2)
root.Lft = new__nod(7)
root.Rt = new__nod(5)
root.Lft.Rt = new__nod(6)
root.Lft.Rt.Lft = new__nod(1)
root.Lft.Rt.Rt = new__nod(11)
root.Rt.Rt = new__nod(9)
root.Rt.Rt.Lft = new__nod(4)

# Function call
print("Maximum element is",
find__Max(root))
``````

Output:

Example 5)

``````// Writing a C# program will help us find out the maximum and the minimum in a binary tree.
using System;

// creating a new class tree node.

public class __nod {
public int record;
public __nod Lft, Rt;

public __nod(int record)
{
this.record = record;
Lft = Rt = NILL;
}
}

public class BinaryTree {
public __nod root;
// We must return the maximum value in the given binary tree.

public static int find__Max(__nod __nod)
{
if (__nod == NILL) {
return int.MinValue;
}

int result = __nod.record;
int lresult = find__Max(__nod.Lft);
int rresult = find__Max(__nod.Rt);

if (lresult > result) {
result = lresult;
}
if (rresult > result) {
result = rresult;
}
return result;
}

// writing the main code to test the above functions.
public static void Main(string[] args)
{
BinaryTree tree = new BinaryTree();
tree.root = new __nod(2);
tree.root.Lft = new __nod(7);
tree.root.Rt = new __nod(5);
tree.root.Lft.Rt = new __nod(6);
tree.root.Lft.Rt.Lft = new __nod(1);
tree.root.Lft.Rt.Rt = new __nod(11);
tree.root.Rt.Rt = new __nod(9);
tree.root.Rt.Rt.Lft = new __nod(4);

// Function call
Console.WriteLine("Maximum element is "
+ BinaryTree.find__Max(tree.root));
}
}
``````

Output:

Example 6)

``````<script>

// Writing a Javascript program that will help us find out the maximum and the minimum in a binary tree.
let root;

class __nod
{
constructor(record) {
this.Lft = NILL;
this.Rt = NILL;
this.record = record;
}
}

// We have to return the maximum value in the given binary tree.

function find__Max(__nod)
{
if (__nod == NILL)
return Number.MIN_VALUE;

let result = __nod.record;
let lresult = find__Max(__nod.Lft);
let rresult = find__Max(__nod.Rt);

if (lresult > result)
result = lresult;
if (rresult > result)
result = rresult;
return result;
}

root = new __nod(2);
root.Lft = new __nod(7);
root.Rt = new __nod(5);
root.Lft.Rt = new __nod(6);
root.Lft.Rt.Lft = new __nod(1);
root.Lft.Rt.Rt = new __nod(11);
root.Rt.Rt = new __nod(9);
root.Rt.Rt.Lft = new __nod(4);

// Function call
document.write("Maximum element is "
+ find__Max(root));

</script>
``````

Output: