Open In App

Find the node with maximum value in a Binary Search Tree using recursion

Improve
Improve
Like Article
Like
Save
Share
Report

Given a Binary Search Tree, the task is to find the node with maximum value.

Examples: 

Input: 
 

BST_LCA

Output: 22 

Approach: Just traverse the node from root to right recursively until right is NULL. The node whose right is NULL is the node with the maximum value.

Below is the implementation of the above approach:  

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
/* A binary tree node has data, pointer to left child
and a pointer to right child */
struct node {
    int data;
    struct node* left;
    struct node* right;
};
 
/* Helper function that allocates a new node
with the given data and NULL left and right
pointers. */
struct node* newNode(int data)
{
    struct node* node = (struct node*)
        malloc(sizeof(struct node));
    node->data = data;
    node->left = NULL;
    node->right = NULL;
 
    return (node);
}
 
/* Give a binary search tree and a number,
inserts a new node with the given number in
the correct place in the tree. Returns the new
root pointer which the caller should then use
(the standard trick to avoid using reference
parameters). */
struct node* insert(struct node* node, int data)
{
 
    /* 1. If the tree is empty, return a new,
    single node */
    if (node == NULL)
        return (newNode(data));
    else {
 
        /* 2. Otherwise, recur down the tree */
        if (data <= node->data)
            node->left = insert(node->left, data);
        else
            node->right = insert(node->right, data);
 
        /* return the (unchanged) node pointer */
        return node;
    }
}
 
// Function to return the minimum node
// in the given binary search tree
int maxValue(struct node* node)
{
    if (node->right == NULL)
        return node->data;
    return maxValue(node->right);
}
 
// Driver code
int main()
{
 
    // Create the BST
    struct node* root = NULL;
    root = insert(root, 4);
    insert(root, 2);
    insert(root, 1);
    insert(root, 3);
    insert(root, 6);
    insert(root, 5);
 
    cout << maxValue(root);
 
    return 0;
}


Java




// Java implementation of the approach
class GFG
{
 
/* A binary tree node has data,
   pointer to left child and a
   pointer to right child */
static class node
{
    int data;
    node left;
    node right;
};
 
/* Helper function that allocates a new node
with the given data and null left and right
pointers. */
static node newNode(int data)
{
    node node = new node();
    node.data = data;
    node.left = null;
    node.right = null;
 
    return (node);
}
 
/* Give a binary search tree and a number,
inserts a new node with the given number in
the correct place in the tree. Returns the new
root pointer which the caller should then use
(the standard trick to avoid using reference
parameters). */
static node insert(node node, int data)
{
 
    /* 1. If the tree is empty, return a new,
    single node */
    if (node == null)
        return (newNode(data));
    else
    {
 
        /* 2. Otherwise, recur down the tree */
        if (data <= node.data)
            node.left = insert(node.left, data);
        else
            node.right = insert(node.right, data);
 
        /* return the (unchanged) node pointer */
        return node;
    }
}
 
// Function to return the minimum node
// in the given binary search tree
static int maxValue(node node)
{
    if (node.right == null)
        return node.data;
    return maxValue(node.right);
}
 
// Driver code
public static void main(String args[])
{
 
    // Create the BST
    node root = null;
    root = insert(root, 4);
    root = insert(root, 2);
    root = insert(root, 1);
    root = insert(root, 3);
    root = insert(root, 6);
    root = insert(root, 5);
 
    System.out.println(maxValue(root));
}
}
 
// This code is contributed by Arnab Kundu


Python3




# Python3 implementation of the approach
 
# A binary tree node has data,
# pointer to left child and
# a pointer to right child
# Linked list node
class Node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None
 
# Helper function that allocates
# a new node with the given data
# and None left and right pointers.
def newNode(data):
    node = Node(0)
    node.data = data
    node.left = None
    node.right = None
 
    return (node)
 
# Give a binary search tree and a number,
# inserts a new node with the given number in
# the correct place in the tree. Returns the new
# root pointer which the caller should then use
# (the standard trick to avoid using reference
# parameters).
def insert(node,data):
 
    # 1. If the tree is empty, 
    # return a new, single node
    if (node == None):
        return (newNode(data))
    else :
 
        # 2. Otherwise, recur down the tree
        if (data <= node.data):
            node.left = insert(node.left, data)
        else:
            node.right = insert(node.right, data)
 
        # return the (unchanged) node pointer */
        return node
     
# Function to return the minimum node
# in the given binary search tree
def maxValue(node):
 
    if (node.right == None):
        return node.data
    return maxValue(node.right)
 
# Driver Code
if __name__=='__main__':
 
    # Create the BST
    root = None
    root = insert(root, 4)
    root = insert(root, 2)
    root = insert(root, 1)
    root = insert(root, 3)
    root = insert(root, 6)
    root = insert(root, 5)
 
    print (maxValue(root))
 
# This code is contributed by Arnab Kundu


C#




// C# implementation of the approach
using System;
     
class GFG
{
 
/* A binary tree node has data,
pointer to left child and a
pointer to right child */
public class node
{
    public int data;
    public node left;
    public node right;
};
 
/* Helper function that allocates a new node
with the given data and null left and right
pointers. */
static node newNode(int data)
{
    node node = new node();
    node.data = data;
    node.left = null;
    node.right = null;
 
    return (node);
}
 
/* Give a binary search tree and a number,
inserts a new node with the given number in
the correct place in the tree. Returns the new
root pointer which the caller should then use
(the standard trick to avoid using reference
parameters). */
static node insert(node node, int data)
{
 
    /* 1. If the tree is empty, return a new,
    single node */
    if (node == null)
        return (newNode(data));
    else
    {
 
        /* 2. Otherwise, recur down the tree */
        if (data <= node.data)
            node.left = insert(node.left, data);
        else
            node.right = insert(node.right, data);
 
        /* return the (unchanged) node pointer */
        return node;
    }
}
 
// Function to return the minimum node
// in the given binary search tree
static int maxValue(node node)
{
    if (node.right == null)
        return node.data;
    return maxValue(node.right);
}
 
// Driver code
public static void Main(String []args)
{
 
    // Create the BST
    node root = null;
    root = insert(root, 4);
    root = insert(root, 2);
    root = insert(root, 1);
    root = insert(root, 3);
    root = insert(root, 6);
    root = insert(root, 5);
 
    Console.WriteLine(maxValue(root));
}
}
 
/* This code contributed by PrinciRaj1992 */


Javascript




<script>
 
// Javascript implementation of the approach
 
/* A binary tree node has data,
pointer to left child and a
pointer to right child */
class node
{
    constructor(data)
    {
        this.left = null;
        this.right = null;
        this.data = data;
    }
}
 
/* Helper function that allocates a new node
with the given data and null left and right
pointers. */
function newNode(data)
{
    let Node = new node(data);
    return (Node);
}
 
/* Give a binary search tree and a number,
inserts a new node with the given number in
the correct place in the tree. Returns the new
root pointer which the caller should then use
(the standard trick to avoid using reference
parameters). */
function insert(Node, data)
{
     
    /* 1. If the tree is empty, return a new,
    single node */
    if (Node == null)
        return (newNode(data));
    else
    {
 
        /* 2. Otherwise, recur down the tree */
        if (data <= Node.data)
            Node.left = insert(Node.left, data);
        else
            Node.right = insert(Node.right, data);
 
        /* Return the (unchanged) node pointer */
        return Node;
    }
}
 
// Function to return the minimum node
// in the given binary search tree
function maxValue(Node)
{
    if (Node.right == null)
        return Node.data;
         
    return maxValue(Node.right);
}
 
// Driver code
 
// Create the BST
let root = null;
root = insert(root, 4);
root = insert(root, 2);
root = insert(root, 1);
root = insert(root, 3);
root = insert(root, 6);
root = insert(root, 5);
 
document.write(maxValue(root));
 
// This code is contributed by divyeshrabadiya07
 
</script>


Output: 

6

Time Complexity: O(n), worst case happens for right skewed trees.

Auxiliary Space: O(n), maximum number of stack frames that could be present in memory is ‘n’
 



Last Updated : 25 May, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads