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

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

Examples:

Input:

Output: 4

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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

Below is the implementation of the above approach:

 `// C++ implementation of the approach ` `#include ` `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` `minValue(``struct` `node* node) ` `{ ` `    ``if` `(node->left == NULL) ` `        ``return` `node->data; ` `    ``return` `minValue(node->left); ` `} ` ` `  `// 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 << minValue(root); ` ` `  `    ``return` `0; ` `} `

 `// Java Implementation of the above 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` `minValue(Node node) ` `{ ` `    ``if` `(node.left == ``null``) ` `        ``return` `node.data; ` `    ``return` `minValue(node.left); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``// Create the BST ` `    ``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(minValue(root)); ` ` `  `} ` `} ` ` `  `// This code has been contributed by 29AjayKumar `

 `# Python3 Implementation of ` `# the above approach ` `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 null left and right pointers. ` `def` `insert(node, data): ` `    ``if` `node ``is` `None` `: ` `        ``return` `Node(data) ` `    ``else``: ` `        ``if` `data <``=` `node.data: ` `            ``node.left ``=` `insert(node.left, data) ` `        ``else``: ` `            ``node.right ``=` `insert(node.right, data) ` ` `  `        ``return` `node ` ` `  `# Function to return the minimum node ` `# in the given binary search tree ` `def` `minValue(node): ` `    ``if` `node.left ``=``=` `None``: ` `        ``return` `node.data ` `    ``return` `minValue(node.left) ` ` `  `# Driver code ` `if` `__name__ ``=``=` `"__main__"` `: ` `     `  `    ``# Create the BST ` `    ``root ``=` `None` `    ``root ``=` `insert(root, ``4``) ` `    ``insert(root, ``2``) ` `    ``insert(root, ``1``) ` `    ``insert(root, ``3``) ` `    ``insert(root, ``6``) ` `    ``insert(root, ``5``) ` ` `  `    ``print``(minValue(root)) ` ` `  `# This code is contributed by vinayak `

 `// C# Implementation of the above 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` `minValue(Node node) ` `{ ` `    ``if` `(node.left == ``null``) ` `        ``return` `node.data; ` `    ``return` `minValue(node.left); ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String []args) ` `{ ` `    ``// Create the BST ` `    ``Node root = ``null``; ` `    ``root = insert(root, 4); ` `    ``insert(root, 2); ` `    ``insert(root, 1); ` `    ``insert(root, 3); ` `    ``insert(root, 6); ` `    ``insert(root, 5); ` ` `  `    ``Console.WriteLine(minValue(root)); ` ` `  `} ` `} ` ` `  `// This code contributed by Rajput-Ji `

Output:
```1
```

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

Check out this Author's contributed articles.

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.

Improved By : 29AjayKumar, Rajput-Ji, itsvinayak

Article Tags :