[Naive Approach] Using Inorder Traversal - O(n) Time and O(n) Space
The idea is to use the property of BST which says inorder 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++
// C++ code to find minimum value in BST// using inorder traversal#include<bits/stdc++.h>usingnamespacestd;structNode{intdata;Node*left,*right;Node(intval){data=val;left=right=nullptr;}};// Recursive function to solve and store elements // in a vectorvoidinorder(Node*root,vector<int>&sortedInorder){// Base Caseif(root==nullptr)return;// Traverse left subtreeinorder(root->left,sortedInorder);// Store the current node's datasortedInorder.push_back(root->data);// Traverse right subtreeinorder(root->right,sortedInorder);}// Function to find the minimum value in BSTintminValue(Node*root){if(root==nullptr){return-1;}vector<int>sortedInorder;// Call the recursive inorder functioninorder(root,sortedInorder);// Return the first element, which is the minimumreturnsortedInorder[0];}intmain(){// Representation of input binary search tree// 5// / \ // 4 6// / \ // 3 7// / // 1Node*root=newNode(5);root->left=newNode(4);root->right=newNode(6);root->left->left=newNode(3);root->right->right=newNode(7);root->left->left->left=newNode(1);cout<<minValue(root)<<"\n";return0;}
C
// C code to find minimum value in BST// using inorder traversal#include<stdio.h>#include<stdlib.h>#include<limits.h>structNode{intdata;structNode*left;structNode*right;};// Recursive function to traverse the tree // and store elements in a vectorvoidinorder(structNode*root,int*sortedInorder,int*index){// Base Caseif(root==NULL)return;// Traverse left subtreeinorder(root->left,sortedInorder,index);// Store the current node's datasortedInorder[(*index)++]=root->data;// Traverse right subtreeinorder(root->right,sortedInorder,index);}// Function to find the minimum value in a BSTintminValue(structNode*root){if(root==NULL){return-1;}// Create an array to hold inorder elementsintsortedInorder[20000];intindex=0;// Call the recursive inorder functioninorder(root,sortedInorder,&index);// Return the first element, which is the minimumreturnsortedInorder[0];}structNode*createNode(intval){structNode*node=(structNode*)malloc(sizeof(structNode));node->data=val;node->left=node->right=NULL;returnnode;}intmain(){// Representation of input binary search tree// 5// / \ // 4 6// / \ // 3 7// /// 1structNode*root=createNode(5);root->left=createNode(4);root->right=createNode(6);root->left->left=createNode(3);root->right->right=createNode(7);root->left->left->left=createNode(1);printf("%d\n",minValue(root));return0;}
Java
// Java code to find minimum value in BST// using inorder traversalimportjava.util.ArrayList;classNode{intdata;Nodeleft,right;Node(intval){data=val;left=right=null;}}classGfG{staticvoidinorder(Noderoot,ArrayList<Integer>sortedInorder){// Base Caseif(root==null)return;// Traverse left subtreeinorder(root.left,sortedInorder);// Store the current node's datasortedInorder.add(root.data);// Traverse right subtreeinorder(root.right,sortedInorder);}staticintminValue(Noderoot){if(root==null){return-1;}// Create an ArrayList to hold inorder elementsArrayList<Integer>sortedInorder=newArrayList<>();// Call the recursive inorder functioninorder(root,sortedInorder);// Return the first element, which is the minimumreturnsortedInorder.get(0);}publicstaticvoidmain(String[]args){// Representation of input binary search tree// 5// / \// 4 6// / \// 3 7// /// 1Noderoot=newNode(5);root.left=newNode(4);root.right=newNode(6);root.left.left=newNode(3);root.right.right=newNode(7);root.left.left.left=newNode(1);System.out.println(minValue(root));}}
Python
# Python code to find minimum value in BST# using inorder traversalclassNode:def__init__(self,data):self.data=dataself.left=Noneself.right=Nonedefinorder(root,sorted_inorder):# Base CaseifrootisNone:return# Traverse left subtreeinorder(root.left,sorted_inorder)# Store the current node's datasorted_inorder.append(root.data)# Traverse right subtreeinorder(root.right,sorted_inorder)defminValue(root):ifrootisNone:return-1# Using a list to hold inorder elementssorted_inorder=[]# Call the recursive inorder functioninorder(root,sorted_inorder)# Return the first element, which is the minimumreturnsorted_inorder[0]if__name__=="__main__":# Representation of input binary search tree# 5# / \# 4 6# / \# 3 7# /# 1root=Node(5)root.left=Node(4)root.right=Node(6)root.left.left=Node(3)root.right.right=Node(7)root.left.left.left=Node(1)print(minValue(root))
C#
// C# code to find minimum value in BST// using inorder traversalusingSystem;usingSystem.Collections.Generic;classNode{publicintdata;publicNodeleft,right;publicNode(intval){data=val;left=right=null;}}classGfG{staticvoidInorder(Noderoot,List<int>sortedInorder){// Base Caseif(root==null)return;// Traverse left subtreeInorder(root.left,sortedInorder);// Store the current node's datasortedInorder.Add(root.data);// Traverse right subtreeInorder(root.right,sortedInorder);}staticintMinValue(Noderoot){if(root==null){return-1;}// Create a list to hold inorder elementsList<int>sortedInorder=newList<int>();// Call the recursive inorder functionInorder(root,sortedInorder);// Return the first element, which is the minimumreturnsortedInorder[0];}staticvoidMain(string[]args){// Representation of input binary search tree// 5// / \// 4 6// / \// 3 7// /// 1Noderoot=newNode(5);root.left=newNode(4);root.right=newNode(6);root.left.left=newNode(3);root.right.right=newNode(7);root.left.left.left=newNode(1);Console.WriteLine(MinValue(root));}}
JavaScript
// JavaScript code to find minimum value in BST// using inorder traversalclassNode{constructor(data){this.data=data;this.left=null;this.right=null;}}functioninorder(root,sortedInorder){// Base caseif(root===null)return;// Traverse left subtreeinorder(root.left,sortedInorder);// Store the current node's datasortedInorder.push(root.data);// Traverse right subtreeinorder(root.right,sortedInorder);}functionminValue(root){if(root===null){return-1;}// Create an array to hold inorder elementsletsortedInorder=[];// Call the recursive inorder functioninorder(root,sortedInorder);// Return the first element, which is the minimumreturnsortedInorder[0];}// Representation of input binary search tree// 5// / \// 4 6// / \// 3 7// /// 1letroot=newNode(5);root.left=newNode(4);root.right=newNode(6);root.left.left=newNode(3);root.right.right=newNode(7);root.left.left.left=newNode(1);console.log(minValue(root));
Output
1
Time Complexity: O(n), since we traversed through all the elements in aBST. Auxiliary Space: O(n), we are storing all the n nodes in an array.
[Alternate Approach] Using Recursion- O(n) Time and O(n) Space
[Expected Approach] Traversing Only Left Edges - O(h) Time and O(1) Space
The idea is that in a Binary Search Tree (BST), the left child of a node is always smaller than the root. This ensures that the node whose left pointer is NULL must hold the minimum value in the tree. The leftmost node will always contain the smallest element.
Below is the implementation of the above approach:
C++
// C++ code to find minimum value in BST// using iteration#include<bits/stdc++.h>usingnamespacestd;structNode{intdata;Node*left,*right;Node(intval){data=val;left=right=nullptr;}};intminValue(Node*root){// base caseif(root==nullptr){return-1;}Node*curr=root;// leftmost node is minimum so we move in BST till// left node is not nullptrwhile(curr->left!=nullptr){curr=curr->left;}// returning the data at the leftmost nodereturncurr->data;}intmain(){// Representation of input binary search tree// 5// / \ // 4 6// / \ // 3 7// / // 1Node*root=newNode(5);root->left=newNode(4);root->right=newNode(6);root->left->left=newNode(3);root->right->right=newNode(7);root->left->left->left=newNode(1);cout<<minValue(root)<<"\n";return0;}
C
// C code to find minimum value in BST// using iteration#include<stdio.h>#include<stdlib.h>#include<limits.h>structNode{intdata;structNode*left;structNode*right;};// Function to find minimum value in BSTintminValue(structNode*root){// base caseif(root==NULL){return-1;}structNode*curr=root;// leftmost node is minimum, so move // till left is not NULLwhile(curr->left!=NULL){curr=curr->left;}// returning the data at the leftmost nodereturncurr->data;}structNode*createNode(intval){structNode*node=(structNode*)malloc(sizeof(structNode));node->data=val;node->left=node->right=NULL;returnnode;}intmain(){// Representation of input binary search tree// 5// / \ // 4 6// / \ // 3 7// /// 1structNode*root=createNode(5);root->left=createNode(4);root->right=createNode(6);root->left->left=createNode(3);root->right->right=createNode(7);root->left->left->left=createNode(1);printf("%d\n",minValue(root));return0;}
Java
// Java code to find minimum value in BST// using iterationclassNode{intdata;Nodeleft,right;Node(intval){data=val;left=right=null;}}publicclassGfG{publicstaticintminValue(Noderoot){// base caseif(root==null){return-1;}Nodecurr=root;// leftmost node is minimum, so move till // left is not nullwhile(curr.left!=null){curr=curr.left;}// returning the data at the leftmost nodereturncurr.data;}publicstaticvoidmain(String[]args){// Representation of input binary search tree// 5// / \// 4 6// / \// 3 7// /// 1Noderoot=newNode(5);root.left=newNode(4);root.right=newNode(6);root.left.left=newNode(3);root.right.right=newNode(7);root.left.left.left=newNode(1);System.out.println(minValue(root));}}
Python
# Python code to find minimum value in BST# using using iterationlclassNode:def__init__(self,data):self.data=dataself.left=Noneself.right=None# Function to find the minimum value in BSTdefminValue(root):# base caseifrootisNone:return-1curr=root# leftmost node is minimum, so move # till left is not Nonewhilecurr.leftisnotNone:curr=curr.left# returning the data at the leftmost nodereturncurr.dataif__name__=="__main__":# Representation of input binary search tree# 5# / \# 4 6# / \# 3 7# /# 1root=Node(5)root.left=Node(4)root.right=Node(6)root.left.left=Node(3)root.right.right=Node(7)root.left.left.left=Node(1)print(minValue(root))
C#
// C# code to find minimum value in BST// using iterationusingSystem;usingSystem.Collections.Generic;classNode{publicintdata;publicNodeleft,right;publicNode(intval){data=val;left=right=null;}}classGfG{staticintminValue(Noderoot){// base caseif(root==null){return-1;}Nodecurr=root;// leftmost node is minimum, so move // till left is not nullwhile(curr.left!=null){curr=curr.left;}// returning the data at the leftmost nodereturncurr.data;}staticvoidMain(string[]args){// Representation of input binary search tree// 5// / \// 4 6// / \// 3 7// /// 1Noderoot=newNode(5);root.left=newNode(4);root.right=newNode(6);root.left.left=newNode(3);root.right.right=newNode(7);root.left.left.left=newNode(1);Console.WriteLine(minValue(root));}}
JavaScript
// Javascript code to find minimum value in BST// using iterationclassNode{constructor(data){this.data=data;this.left=null;this.right=null;}}// Function to find the minimum value in BSTfunctionminValue(root){// base caseif(root===null){return-1;}letcurr=root;// leftmost node is minimum, so move till // left is not nullwhile(curr.left!==null){curr=curr.left;}// returning the data at the leftmost nodereturncurr.data;}// Representation of input binary search tree// 5// / \// 4 6// / \// 3 7// /// 1letroot=newNode(5);root.left=newNode(4);root.right=newNode(6);root.left.left=newNode(3);root.right.right=newNode(7);root.left.left.left=newNode(1);// Output the minimum value in the BSTconsole.log(minValue(root));
Output
1
Time Complexity: O(h), where h is the height of the BST. Worst case happens for left skewed trees, in that case complexity becomes O(n). Auxiliary Space: O(1), we are not using any extra memory.