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

25 May, 2022
Given a Binary Search Tree, the task is to find the node with maximum value.
Examples: 
Input:  
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++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; /* A binary tree node has data, pointer to left childand a pointer to right child */struct node {    int data;    struct node* left;    struct node* right;}; /* Helper function that allocates a new nodewith the given data and NULL left and rightpointers. */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 inthe correct place in the tree. Returns the newroot pointer which the caller should then use(the standard trick to avoid using referenceparameters). */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 treeint maxValue(struct node* node){    if (node->right == NULL)        return node->data;    return maxValue(node->right);} // Driver codeint 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 implementation of the approachclass 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 nodewith the given data and null left and rightpointers. */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 inthe correct place in the tree. Returns the newroot pointer which the caller should then use(the standard trick to avoid using referenceparameters). */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 treestatic int maxValue(node node){    if (node.right == null)        return node.data;    return maxValue(node.right);} // Driver codepublic 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 implementation of the approach # A binary tree node has data,# pointer to left child and# a pointer to right child# Linked list nodeclass 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 treedef maxValue(node):     if (node.right == None):        return node.data    return maxValue(node.right) # Driver Codeif __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# implementation of the approachusing System;     class GFG{ /* A binary tree node has data,pointer to left child and apointer to right child */public class node{    public int data;    public node left;    public node right;}; /* Helper function that allocates a new nodewith the given data and null left and rightpointers. */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 inthe correct place in the tree. Returns the newroot pointer which the caller should then use(the standard trick to avoid using referenceparameters). */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 treestatic int maxValue(node node){    if (node.right == null)        return node.data;    return maxValue(node.right);} // Driver codepublic 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 */
<script> // Javascript implementation of the approach /* A binary tree node has data,pointer to left child and apointer to right child */class node{    constructor(data)    {        this.left = null;        this.right = null;        this.data = data;    }} /* Helper function that allocates a new nodewith the given data and null left and rightpointers. */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 inthe correct place in the tree. Returns the newroot pointer which the caller should then use(the standard trick to avoid using referenceparameters). */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 treefunction maxValue(Node){    if (Node.right == null)        return Node.data;             return maxValue(Node.right);} // Driver code // Create the BSTlet 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’ 
andrew1234
princiraj1992
divyeshrabadiya07
jayanth_mkv
Binary Search Tree
Recursion
Tree
Recursion
Binary Search Tree
Tree
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.