Find the node with maximum value in a Binary Search Tree using recursion Last Updated : 25 May, 2022 Comments Improve Suggest changes Like Article Like Report 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; } 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' Comment More infoAdvertise with us Next Article Find the node with maximum value in a Binary Search Tree using recursion G gp6 Follow Improve Article Tags : Misc Tree Binary Search Tree Recursion DSA +1 More Practice Tags : Binary Search TreeMiscRecursionTree Similar Reads Find the node with minimum value in a Binary Search Tree using recursion Given the root of a Binary Search Tree, the task is to find the minimum valued element in this given BST.Examples: Input: Output: 1Explanation: The minimum element in the given BST is 1.Input: Output: 2Explanation: The minimum element in the given BST is 2.Approach:The idea is to recursively travers 6 min read Find Maximum Level Sum in Binary Tree using Recursion Given a Binary Tree having positive and negative nodes, the task is to find the maximum sum level in it and print the maximum sum.Examples: Input: 4 / \ 2 -5 / \ / \ -1 3 -2 6 Output: 6 Sum of all nodes of the 1st level is 4. Sum of all nodes of the 2nd level is -3. Sum of all nodes of the 3rd level 10 min read Find maximum and minimum element in binary tree without using recursion or stack or queue Given a binary tree. The task is to find out the maximum and minimum element in a binary tree without using recursion or stack or queue i.e, space complexity should be O(1). Examples: Input : 12 / \ 13 10 / \ 14 15 / \ / \ 21 24 22 23 Output : Max element : 24 Min element : 10 Input : 12 / \ 19 82 / 10 min read Find the minimum Sub-tree with target sum in a Binary search tree Given a binary tree and a target, find the number of nodes in the minimum sub-tree with the given sum equal to the target which is also a binary search tree. Examples: Input: 13 / \ 5 23 / \ / \ N 17 N N / 16Target: 38Output: 3Explanation: 5, 17, 16 is the smallest subtree with length 3. Input: 7 / 9 min read Leaf nodes from Preorder of a Binary Search Tree (Using Recursion) Given Preorder traversal of a Binary Search Tree. Then the task is to print leaf nodes of the Binary Search Tree from the given preorder.Examples : Input : preorder[] = {890, 325, 290, 530, 965};Output : 290 530 965Explanation: Below is the representation of BST using preorder array. Approach:To ide 8 min read Find maximum count of duplicate nodes in a Binary Search Tree Given a Binary Search Tree (BST) with duplicates, find the node (the most frequently occurred element) in the given BST. If the BST contains two or more such nodes, print any of them. Note: We cannot use any extra space. (Assume that the implicit stack space incurred due to recursion does not count) 9 min read Maximum XOR with given value in the path from root to given node in the tree Given a tree with N distinct nodes from the range [1, n] and two integers x and val. The task is to find the maximum value of any node when XORed with x on the path from the root to val. Examples: Input: val = 6, x = 4 1 / \ 2 3 / \ 4 5 / 6 Output: 7 the path is 1 -> 3 -> 5 -> 6 1 ^ 4 = 5 3 9 min read Maximum width of a Binary Tree with null values | Set 2 Pre-requisite: Maximum width of a Binary Tree with null value | Set 1 Given a Binary Tree consisting of N nodes, the task is to find the maximum width of the given tree without using recursion, where the maximum width is defined as the maximum of all the widths at each level of the given Tree. Note: 8 min read Diameter of a Binary Tree using Top Down Recursion Given a binary tree, the task is to determine the diameter of the tree. The diameter/width of a tree is defined as the number of edges on the longest path between any two nodes. Examples:Input: Output: 2Explanation: The longest path has 2 edges (node 2 -> node 1 -> node 3). Input: Output: 4Exp 8 min read Maximum number in Binary tree of binary values Given a binary tree consisting of nodes, each containing a binary value of either 0 or 1, the task is to find the maximum decimal number that can be formed by traversing from the root to a leaf node. The maximum number is achieved by concatenating the binary values along the path from the root to a 6 min read Like