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

For the above tree, we start with 20, then we move right to 22. We keep on moving to the right until we see NULL. Since the right of 22 is NULL, 22 is the node with the maximum value.
Approach: This is quite simple. Just traverse the node from root to right recursively until the right is NULL. The node whose right is NULL is the node with the maximum value.
C++
#include <bits/stdc++.h>
using namespace std;
struct node {
int data;
struct node* left;
struct node* right;
};
struct node* newNode( int data)
{
struct node* newnode = new node();
newnode->data = data;
newnode->left = NULL;
newnode->right = NULL;
return (newnode);
}
struct node* insert( struct node* node, int data)
{
if (node == NULL)
return (newNode(data));
else {
if (data <= node->data)
node->left = insert(node->left, data);
else
node->right = insert(node->right, data);
return node;
}
}
int maxValue( struct node* node)
{
struct node* current = node;
while (current->right != NULL)
current = current->right;
return (current->data);
}
int main()
{
struct node* root = NULL;
root = insert(root, 4);
insert(root, 2);
insert(root, 1);
insert(root, 3);
insert(root, 6);
insert(root, 5);
cout << "Maximum value in BST is " << maxValue(root);
return 0;
}
|
Java
import java.util.*;
public class GFG {
static class node {
int data;
node left;
node right;
};
static node newNode( int data){
node node = new node();
node.data = data;
node.left = null ;
node.right = null ;
return node;
}
static node insert(node node, int data){
if (node == null )
return (newNode(data));
else {
if (data <= node.data)
node.left = insert(node.left, data);
else
node.right = insert(node.right, data);
return node;
}
}
static int maxValue(node node){
node current = node;
while (current.right != null )
current = current.right;
return current.data;
}
public static void main(String[] args){
node root = null ;
root = insert(root, 4 );
insert(root, 2 );
insert(root, 1 );
insert(root, 3 );
insert(root, 6 );
insert(root, 5 );
System.out.println( "Maximum value in BST is " + maxValue(root));
}
}
|
Python3
import sys
import math
class Node:
def __init__( self ,data):
self .data = data
self .left = None
self .right = None
def insert(root, data):
if not root:
return Node(data)
if data < root.data:
root.left = insert(root.left, data)
if data > root.data:
root.right = insert(root.right, data)
return root
def maxValue(root):
current = root
while (current.right):
current = current.right
return current.data
if __name__ = = '__main__' :
root = None
root = insert(root, 2 )
root = insert(root, 1 )
root = insert(root, 3 )
root = insert(root, 6 )
root = insert(root, 5 )
print ( "Maximum value in BST is {}" . format (maxValue(root)))
|
C#
using System;
class GFG
{
public class node
{
public int data;
public node left;
public node right;
};
static node newNode( int data)
{
node node = new node();
node.data = data;
node.left = null ;
node.right = null ;
return (node);
}
static node insert(node node, int data)
{
if (node == null )
return (newNode(data));
else
{
if (data <= node.data)
node.left = insert(node.left, data);
else
node.right = insert(node.right, data);
return node;
}
}
static int maxValue(node node)
{
node current = node;
while (current.right != null )
current = current.right;
return (current.data);
}
public static void Main(String[] args)
{
node root = null ;
root = insert(root, 4);
insert(root, 2);
insert(root, 1);
insert(root, 3);
insert(root, 6);
insert(root, 5);
Console.WriteLine( "Maximum value in BST is " + maxValue(root));
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .left = null ;
this .right = null ;
}
}
function newNode(data) {
var node = new Node();
node.data = data;
node.left = null ;
node.right = null ;
return (node);
}
function insert( node , data) {
if (node == null )
return (newNode(data));
else {
if (data <= node.data)
node.left = insert(node.left, data);
else
node.right = insert(node.right, data);
return node;
}
}
function maxValue( node) {
var current = node;
while (current.right != null )
current = current.right;
return (current.data);
}
var root = null ;
root = insert(root, 4);
insert(root, 2);
insert(root, 1);
insert(root, 3);
insert(root, 6);
insert(root, 5);
document.write( "Maximum value in BST is " + maxValue(root));
</script>
|
OutputMaximum value in BST is 6
Complexity Analysis:
- Time Complexity: O(h), where h is the height of the BST.
- Auxiliary Space: O(1)
Using Morris traversal:

Follow the steps below to implement the above idea:
- Initialize a variable max_val to store the maximum value seen so far, and a pointer curr to point to the current node.
- While curr is not NULL, do the following:
- If the left subtree of curr is NULL, update max_val with the value of curr, and move to the right subtree of curr.
- If the left subtree of curr is not NULL, find the predecessor of curr in its left subtree. The predecessor is the rightmost node in the left subtree of curr.
- If the right child of the predecessor is NULL, set it to curr and move to the left child of curr.
- If the right child of the predecessor is curr, restore it to NULL, update max_val with the value of curr, and move to the right child of curr.
- Return max_val.
Implementation:
C++
#include <climits>
#include <iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode( int x)
: val(x)
, left(NULL)
, right(NULL)
{
}
};
int findMaxNode(TreeNode* root)
{
int max_val = INT_MIN;
TreeNode* curr = root;
while (curr != NULL) {
if (curr->left == NULL) {
max_val = max(max_val, curr->val);
curr = curr->right;
}
else {
TreeNode* pred = curr->left;
while (pred->right != NULL
&& pred->right != curr) {
pred = pred->right;
}
if (pred->right == NULL) {
pred->right = curr;
curr = curr->left;
}
else {
pred->right
= NULL;
max_val
= max(max_val,
curr->val);
curr = curr->right;
}
}
}
return max_val;
}
int main()
{
TreeNode* root = new TreeNode(8);
root->left = new TreeNode(3);
root->left->left = new TreeNode(1);
root->left->right = new TreeNode(6);
root->left->right->left = new TreeNode(4);
root->left->right->right = new TreeNode(7);
root->right = new TreeNode(10);
root->right->right = new TreeNode(14);
root->right->right->left = new TreeNode(13);
int max_val = findMaxNode(root);
cout << "The node with maximum value in the binary "
"search tree is "
<< max_val << endl;
return 0;
}
|
Java
import java.lang.*;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode( int val)
{
this .val = val;
this .left = null ;
this .right = null ;
}
TreeNode( int val, TreeNode left, TreeNode right)
{
this .val = val;
this .left = left;
this .right = right;
}
}
class Main {
public static int findMaxNode(TreeNode root)
{
int max_val = -Integer.MAX_VALUE;
TreeNode curr = root;
while (curr != null ) {
if (curr.left == null ) {
max_val = Math.max(max_val, curr.val);
curr
= curr.right;
}
else {
TreeNode pred = curr.left;
while (pred.right != null
&& pred.right != curr) {
pred = pred.right;
}
if (pred.right == null ) {
pred.right = curr;
curr = curr.left;
}
else {
pred.right
= null ;
max_val = Math.max(
max_val,
curr.val);
curr = curr.right;
}
}
}
return max_val;
}
public static void main(String[] args)
{
TreeNode root = new TreeNode( 8 );
root.left = new TreeNode( 3 );
root.left.left = new TreeNode( 1 );
root.left.right = new TreeNode( 6 );
root.left.right.left = new TreeNode( 4 );
root.left.right.right = new TreeNode( 7 );
root.right = new TreeNode( 10 );
root.right.right = new TreeNode( 14 );
root.right.right.left = new TreeNode( 13 );
int max_val = findMaxNode(root);
System.out.println(
"The node with maximum value in the binary search tree is "
+ findMaxNode(root));
}
}
|
Python3
import sys
class TreeNode:
def __init__( self , val = 0 , left = None , right = None ):
self .val = val
self .left = left
self .right = right
def findMaxNode(root: TreeNode) - > int :
max_val = - sys.maxsize
curr = root
while curr:
if not curr.left:
max_val = max (max_val, curr.val)
curr = curr.right
else :
pred = curr.left
while pred.right and pred.right ! = curr:
pred = pred.right
if not pred.right:
pred.right = curr
curr = curr.left
else :
pred.right = None
max_val = max (max_val, curr.val)
curr = curr.right
return max_val
if __name__ = = '__main__' :
root = TreeNode( 8 )
root.left = TreeNode( 3 )
root.left.left = TreeNode( 1 )
root.left.right = TreeNode( 6 )
root.left.right.left = TreeNode( 4 )
root.left.right.right = TreeNode( 7 )
root.right = TreeNode( 10 )
root.right.right = TreeNode( 14 )
root.right.right.left = TreeNode( 13 )
max_val = findMaxNode(root)
print (f "The node with maximum value in the binary search tree is {max_val}" )
|
C#
using System;
public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode( int x)
{
val = x;
left = null ;
right = null ;
}
}
public class Program {
public static int FindMaxNode(TreeNode root)
{
int max_val = int .MinValue;
TreeNode curr = root;
while (curr != null ) {
if (curr.left == null ) {
max_val = Math.Max(max_val, curr.val);
curr
= curr.right;
}
else {
TreeNode pred = curr.left;
while (pred.right != null
&& pred.right != curr) {
pred = pred.right;
}
if (pred.right == null ) {
pred.right = curr;
curr = curr.left;
}
else {
pred.right
= null ;
max_val = Math.Max(
max_val,
curr.val);
curr = curr.right;
}
}
}
return max_val;
}
public static void Main()
{
TreeNode root = new TreeNode(8);
root.left = new TreeNode(3);
root.left.left = new TreeNode(1);
root.left.right = new TreeNode(6);
root.left.right.left = new TreeNode(4);
root.left.right.right = new TreeNode(7);
root.right = new TreeNode(10);
root.right.right = new TreeNode(14);
root.right.right.left = new TreeNode(13);
int max_val = FindMaxNode(root);
Console.WriteLine( "The node with maximum value in "
+ "the binary search tree is "
+ max_val);
}
}
|
OutputThe node with maximum value in the binary search tree is 14
Time Complexity: O(n),In the worst case, the algorithm visits every node in the binary search tree once.
At each node, the algorithm performs a constant amount of work to find the predecessor node and update the temporary links.
Therefore, the time complexity of the Morris Traversal approach is O(n), where n is the number of nodes in the binary search tree.
Space Complexity: O(1)The Morris Traversal approach uses only a constant amount of extra space to store the temporary links between some nodes.
Therefore, the space complexity of the Morris Traversal approach is O(1)