Find the node with maximum value in a Binary Search Tree using recursion
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++
// 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; } |
chevron_right
filter_none
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 |
chevron_right
filter_none
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 */ |
chevron_right
filter_none
Output:
6
Time Complexity: O(n), worst case happens for right skewed trees.
Recommended Posts:
- Find the node with minimum value in a Binary Search Tree using recursion
- Find the node with maximum value in a Binary Search Tree
- Find the node with minimum value in a Binary Search Tree
- Find maximum and minimum element in binary tree without using recursion or stack or queue
- Find the maximum node at a given level in a binary tree
- Leaf nodes from Preorder of a Binary Search Tree (Using Recursion)
- Search a node in Binary Tree
- Insert a node in Binary Search Tree Iteratively
- Postorder predecessor of a Node in Binary Search Tree
- Get maximum left node in binary tree
- Maximum difference between node and its ancestor in Binary Tree
- Sum and Product of minimum and maximum element of Binary Search Tree
- Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree
- Find mirror of a given node in Binary tree
- Find the Deepest Node in a Binary Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




