Write a function to find the node with minimum value in a Binary Search Tree.
Example:
Input:

first example BST
Output: 8
Input:

second example BST
Output: 10
Brute Force Approach: To solve the problem follow the below idea:
The in-order traversal of a binary search tree always returns the value of nodes in sorted order. So the 1st value in the sorted vector will be the minimum value which is the answer.
Below is the implementation of the above approach:
C++14
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;
struct node {
int data;
struct node* left;
struct node* right;
};
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);
}
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;
}
}
void inorder( struct node* node, vector< int >& sortedInorder)
{
if (node == NULL)
return ;
inorder(node->left, sortedInorder);
sortedInorder.push_back(node->data);
inorder(node->right, sortedInorder);
}
int main()
{
struct node* root = NULL;
root = insert(root, 4);
insert(root, 2);
insert(root, 1);
insert(root, 3);
insert(root, 6);
insert(root, 4);
insert(root, 5);
vector< int > sortedInorder;
inorder(
root,
sortedInorder);
printf ( "\n Minimum value in BST is %d" ,
sortedInorder[0]);
getchar ();
return 0;
}
|
Java
import java.util.*;
class Node {
int data;
Node left, right;
public Node( int data)
{
this .data = data;
this .left = null ;
this .right = null ;
}
}
class Main {
public static Node insert(Node node, int data)
{
if (node == null ) {
return new Node(data);
}
else {
if (data <= node.data) {
node.left = insert(node.left, data);
}
else {
node.right = insert(node.right, data);
}
return node;
}
}
public static void inorder(Node node,
List<Integer> sortedInorder)
{
if (node == null ) {
return ;
}
inorder(node.left, sortedInorder);
sortedInorder.add(node.data);
inorder(node.right, sortedInorder);
}
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, 4 );
insert(root, 5 );
List<Integer> sortedInorder
= new ArrayList<Integer>();
inorder(root, sortedInorder);
System.out.printf( "\n Minimum value in BST is %d" ,
sortedInorder.get( 0 ));
}
}
|
Python3
from typing import List
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def insert(node: Node, data: int ) - > Node:
if not node:
return Node(data)
if data < = node.data:
node.left = insert(node.left, data)
else :
node.right = insert(node.right, data)
return node
def inorder(node: Node, sorted_inorder: List [ int ]) - > None :
if not node:
return
inorder(node.left, sorted_inorder)
sorted_inorder.append(node.data)
inorder(node.right, sorted_inorder)
if __name__ = = '__main__' :
root = None
root = insert(root, 4 )
insert(root, 2 )
insert(root, 1 )
insert(root, 3 )
insert(root, 6 )
insert(root, 4 )
insert(root, 5 )
sorted_inorder = []
inorder(root, sorted_inorder)
print (f "Minimum value in BST is {sorted_inorder[0]}" )
|
C#
using System;
using System.Collections.Generic;
class Node {
public int data;
public Node left, right;
public Node( int data)
{
this .data = data;
this .left = null ;
this .right = null ;
}
}
class MainClass {
public static Node insert(Node node, int data)
{
if (node == null ) {
return new Node(data);
}
else {
if (data <= node.data) {
node.left = insert(node.left, data);
}
else {
node.right = insert(node.right, data);
}
return node;
}
}
public static void inorder(Node node,
List< int > sortedInorder)
{
if (node == null ) {
return ;
}
inorder(node.left, sortedInorder);
sortedInorder.Add(node.data);
inorder(node.right, sortedInorder);
}
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, 4);
insert(root, 5);
List< int > sortedInorder = new List< int >();
inorder(root, sortedInorder);
Console.WriteLine( "\n Minimum value in BST is {0}" ,
sortedInorder[0]);
}
}
|
Javascript
class Node {
constructor(data) {
this .data = data;
this .left = null ;
this .right = null ;
}
}
function newNode(data) {
return new Node(data);
}
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 inorder(node, sortedInorder) {
if (node == null ) return ;
inorder(node.left, sortedInorder);
sortedInorder.push(node.data);
inorder(node.right, sortedInorder);
}
let root = null ;
root = insert(root, 4);
insert(root, 2);
insert(root, 1);
insert(root, 3);
insert(root, 6);
insert(root, 4);
insert(root, 5);
let sortedInorder = [];
inorder(root, sortedInorder);
console.log( "Minimum value in BST is " + sortedInorder[0]);
|
Output
Minimum value in BST is 1
Time Complexity: O(n) where n is the number of nodes.
Auxiliary Space: O(n) (for recursive stack space + vector used additionally)
Efficient Approach: To solve the problem follow the below idea:
This is quite simple. Just traverse the node from root to left recursively until left is NULL. The node whose left is NULL is the node with minimum value
Below is the implementation of the above approach:
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* node
= ( struct node*) malloc ( sizeof ( struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
}
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 minValue( struct node* node)
{
struct node* current = node;
while (current->left != NULL) {
current = current->left;
}
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 << "\n Minimum value in BST is " << minValue(root);
getchar ();
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* left;
struct node* right;
};
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);
}
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 minValue( struct node* node)
{
struct node* current = node;
while (current->left != NULL) {
current = current->left;
}
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);
printf ( "\n Minimum value in BST is %d" , minValue(root));
getchar ();
return 0;
}
|
Java
class Node {
int data;
Node left, right;
Node( int d)
{
data = d;
left = right = null ;
}
}
class BinaryTree {
static Node head;
Node insert(Node node, int data)
{
if (node == null ) {
return ( new Node(data));
}
else {
if (data <= node.data) {
node.left = insert(node.left, data);
}
else {
node.right = insert(node.right, data);
}
return node;
}
}
int minvalue(Node node)
{
Node current = node;
while (current.left != null ) {
current = current.left;
}
return (current.data);
}
public static void main(String[] args)
{
BinaryTree tree = new BinaryTree();
Node root = null ;
root = tree.insert(root, 4 );
tree.insert(root, 2 );
tree.insert(root, 1 );
tree.insert(root, 3 );
tree.insert(root, 6 );
tree.insert(root, 5 );
System.out.println( "Minimum value of BST is "
+ tree.minvalue(root));
}
}
|
Python3
class Node:
def __init__( self , key):
self .data = key
self .left = None
self .right = None
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
def minValue(node):
current = node
while (current.left is not None ):
current = current.left
return current.data
if __name__ = = '__main__' :
root = None
root = insert(root, 4 )
insert(root, 2 )
insert(root, 1 )
insert(root, 3 )
insert(root, 6 )
insert(root, 5 )
print ( "\nMinimum value in BST is %d" % (minValue(root)))
|
C#
using System;
public class Node {
public int data;
public Node left, right;
public Node( int d)
{
data = d;
left = right = null ;
}
}
public class BinaryTree {
public static Node head;
public virtual Node insert(Node node, int data)
{
if (node == null ) {
return ( new Node(data));
}
else {
if (data <= node.data) {
node.left = insert(node.left, data);
}
else {
node.right = insert(node.right, data);
}
return node;
}
}
public virtual int minvalue(Node node)
{
Node current = node;
while (current.left != null ) {
current = current.left;
}
return (current.data);
}
public static void Main( string [] args)
{
BinaryTree tree = new BinaryTree();
Node root = null ;
root = tree.insert(root, 4);
tree.insert(root, 2);
tree.insert(root, 1);
tree.insert(root, 3);
tree.insert(root, 6);
tree.insert(root, 5);
Console.WriteLine( "Minimum value of BST is "
+ tree.minvalue(root));
}
}
|
Javascript
<script>
class Node
{
constructor(data) {
this .left = null ;
this .right = null ;
this .data = data;
}
}
let head;
function insert(node, data) {
if (node == null ) {
return ( new Node(data));
} else {
if (data <= node.data) {
node.left = insert(node.left, data);
} else {
node.right = insert(node.right, data);
}
return node;
}
}
function minvalue(node) {
if (node === null ) return null ;
let current = node;
while (current.left != null ) {
current = current.left;
}
return (current.data);
}
let root = null ;
root = insert(root, 4);
insert(root, 2);
insert(root, 1);
insert(root, 3);
insert(root, 6);
insert(root, 5);
document.write( "Minimum value in BST is " + minvalue(root));
</script>
|
PHP
<?php
class node
{
private $node , $left , $right ;
function __construct( $node )
{
$this ->node = $node ;
$left = $right = NULL;
}
function set_left( $left )
{
$this ->left = $left ;
}
function set_right( $right )
{
$this ->right = $right ;
}
function get_left()
{
return $this ->left;
}
function get_right()
{
return $this ->right;
}
function get_node()
{
return $this ->node;
}
}
function get_minimum_value( $node )
{
while ( $node ->get_left() != NULL)
{
$node = $node ->get_left();
}
return $node ->get_node();
}
$node = new node(4);
$lnode = new node(2);
$lnode ->set_left( new node(1));
$lnode ->set_right( new node(3));
$rnode = new node(6);
$rnode ->set_left( new node(5));
$node ->set_left( $lnode );
$node ->set_right( $rnode );
$minimum_value = get_minimum_value( $node );
echo 'Minimum value of BST is ' .
$minimum_value ;
?>
|
Output
Minimum value in BST is 1
Time Complexity: O(n) where n is the number of nodes.
Auxiliary Space: O(1)
Another approach Modified binary search approach:
The basic idea behind this approach is to exploit the properties of a Binary Search Tree (BST). In a BST, the left subtree of a node contains all the nodes with values less than the node's value, and the right subtree contains all the nodes with values greater than the node's value.
- Follow the steps to implement the above idea:
- Start at the root node of the BST.
- If the left child of the current node is NULL, return the value of the current node. This is the minimum element in the BST.
- If the value of the left child is less than the value of the current node, move to the left subtree and repeat step 2.
- If the value of the left child is greater than or equal to the value of the current node, move to the right subtree and repeat step 2.
- Repeat steps 2-4 until you reach a leaf node.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
class Node {
public :
int data;
Node* left;
Node* right;
Node( int data) {
this ->data = data;
left = right = nullptr;
}
};
int findMinimum(Node* root) {
if (root == nullptr) {
return -1;
}
while (root->left != nullptr) {
if (root->left->data < root->data) {
root = root->left;
} else {
root = root->right;
}
}
return root->data;
}
int main() {
Node* root = new Node(4);
root->left = new Node(2);
root->right = new Node(6);
root->left->left = new Node(1);
root->left->right = new Node(3);
root->right->left = new Node(5);
root->right->right = new Node(7);
int minVal = findMinimum(root);
cout << "Minimum element in the BST is: " << minVal << endl;
return 0;
}
|
Java
public class Main {
static class Node {
int data;
Node left, right;
Node( int data) {
this .data = data;
left = right = null ;
}
}
static int findMinimum(Node root) {
if (root == null ) {
return - 1 ;
}
while (root.left != null ) {
if (root.left.data < root.data) {
root = root.left;
} else {
root = root.right;
}
}
return root.data;
}
public static void main(String[] args) {
Node root = new Node( 4 );
root.left = new Node( 2 );
root.right = new Node( 6 );
root.left.left = new Node( 1 );
root.left.right = new Node( 3 );
root.right.left = new Node( 5 );
root.right.right = new Node( 7 );
int minVal = findMinimum(root);
System.out.println( "Minimum element in the BST is: " + minVal);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def find_minimum(root):
if root is None :
return - 1
while root.left is not None :
if root.left.data < root.data:
root = root.left
else :
root = root.right
return root.data
root = Node( 4 )
root.left = Node( 2 )
root.right = Node( 6 )
root.left.left = Node( 1 )
root.left.right = Node( 3 )
root.right.left = Node( 5 )
root.right.right = Node( 7 )
min_val = find_minimum(root)
print ( "Minimum element in the BST is:" , min_val)
|
C#
using System;
public class Node
{
public int data;
public Node left;
public Node right;
public Node( int val)
{
data = val;
left = null ;
right = null ;
}
}
public class GFG
{
public static int FindMinimum(Node root)
{
if (root == null )
{
return -1;
}
while (root.left != null )
{
if (root.left.data < root.data)
{
root = root.left;
}
else
{
root = root.right;
}
}
return root.data;
}
public static void Main( string [] args)
{
Node root = new Node(4);
root.left = new Node(2);
root.right = new Node(6);
root.left.left = new Node(1);
root.left.right = new Node(3);
root.right.left = new Node(5);
root.right.right = new Node(7);
int minVal = FindMinimum(root);
Console.WriteLine( "Minimum element in the BST is: " + minVal);
}
}
|
Javascript
class Node {
constructor(data) {
this .data = data;
this .left = null ;
this .right = null ;
}
}
function findMinimum(root) {
if (root === null ) {
return -1;
}
while (root.left !== null ) {
if (root.left.data < root.data) {
root = root.left;
} else {
root = root.right;
}
}
return root.data;
}
const root = new Node(4);
root.left = new Node(2);
root.right = new Node(6);
root.left.left = new Node(1);
root.left.right = new Node(3);
root.right.left = new Node(5);
root.right.right = new Node(7);
const minVal = findMinimum(root);
console.log( "Minimum element in the BST is:" , minVal);
|
Output
Minimum element in the BST is: 1
Time Complexity: O(log n) , This approach has a time complexity of O(log n), where n is the number of nodes in the BST.
space complexity: O(1).
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
16 Sep, 2023
Like Article
Save Article