[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.
C++
//Driver Code Starts#include<iostream>#include<vector>usingnamespacestd;// Node structureclassNode{public:intdata;Node*left,*right;Node(intval){data=val;left=right=nullptr;}};//Driver Code Ends// Performs inorder traversal of BST // and stores values in sorted ordervoidinorder(Node*root,vector<int>&sortedInorder){// Base Caseif(root==nullptr)return;// Traverse left subtreeinorder(root->left,sortedInorder);sortedInorder.push_back(root->data);// Traverse right subtreeinorder(root->right,sortedInorder);}// Returns the minimum value in a BSTintminValue(Node*root){if(root==nullptr){return-1;}vector<int>sortedInorder;// Get all BST values in sorted orderinorder(root,sortedInorder);returnsortedInorder[0];}//Driver Code Startsintmain(){// Create BST// 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)<<"";return0;}//Driver Code Ends
C
//Driver Code Starts#include<stdio.h>#include<stdlib.h>#include<limits.h>#define MAX_SIZE 100000// Node structurestructNode{intdata;structNode*left;structNode*right;};structNode*createNode(intval){structNode*node=(structNode*)malloc(sizeof(structNode));node->data=val;node->left=node->right=NULL;//Driver Code Endsreturnnode;}// Performs inorder traversal of BST and stores values in sorted ordervoidinorder(structNode*root,int*sortedInorder,int*index){if(root==NULL)return;// Traverse left subtreeinorder(root->left,sortedInorder,index);sortedInorder[(*index)++]=root->data;// Traverse right subtreeinorder(root->right,sortedInorder,index);}// Returns the minimum value in a BSTintminValue(structNode*root){if(root==NULL)return-1;// Array to hold inorder elementsintsortedInorder[MAX_SIZE];intindex=0;// Get all BST values in sorted orderinorder(root,sortedInorder,&index);returnsortedInorder[0];//Driver Code Starts}intmain(){// Create BST// 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",minValue(root));return0;}//Driver Code Ends
Java
//Driver Code Startsimportjava.util.ArrayList;// Node structureclassNode{intdata;Nodeleft,right;Node(intval){data=val;left=right=null;}}classGFG{//Driver Code Ends// Performs inorder traversal of BST // and stores values in sorted orderstaticvoidinorder(Noderoot,ArrayList<Integer>sortedInorder){if(root==null)return;// Traverse left subtreeinorder(root.left,sortedInorder);sortedInorder.add(root.data);// Traverse right subtreeinorder(root.right,sortedInorder);}// Returns the minimum value in a BSTstaticintminValue(Noderoot){if(root==null)return-1;ArrayList<Integer>sortedInorder=newArrayList<>();// Get all BST values in sorted orderinorder(root,sortedInorder);returnsortedInorder.get(0);}//Driver Code Startspublicstaticvoidmain(String[]args){// Create BST// 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));}}//Driver Code Ends
Python
#Driver Code Starts# Node structureclassNode:def__init__(self,data):self.data=dataself.left=Noneself.right=None#Driver Code Ends# Performs inorder traversal of BST# and stores values in sorted orderdefinorder(root,sortedInorder):ifrootisNone:return# Traverse left subtreeinorder(root.left,sortedInorder)sortedInorder.append(root.data)# Traverse right subtreeinorder(root.right,sortedInorder)# Returns the minimum value in a BSTdefminValue(root):ifrootisNone:return-1sortedInorder=[]# Get all BST values in sorted orderinorder(root,sortedInorder)returnsortedInorder[0]#Driver Code Startsif__name__=="__main__":# Create BST# 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))#Driver Code Ends
C#
//Driver Code StartsusingSystem;usingSystem.Collections.Generic;// Node structureclassNode{publicintdata;publicNodeleft,right;publicNode(intval){data=val;left=right=null;}}classGFG{//Driver Code Ends// Performs inorder traversal of BST// and stores values in sorted orderstaticvoidinorder(Noderoot,List<int>sortedInorder){if(root==null)return;// Traverse left subtreeinorder(root.left,sortedInorder);sortedInorder.Add(root.data);// Traverse right subtreeinorder(root.right,sortedInorder);}// Returns the minimum value in a BSTstaticintminValue(Noderoot){if(root==null)return-1;List<int>sortedInorder=newList<int>();// Get all BST values in sorted orderinorder(root,sortedInorder);returnsortedInorder[0];}//Driver Code StartsstaticvoidMain(string[]args){// Create BST// 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));}}//Driver Code Ends
JavaScript
//Driver Code Starts// Node structureclassNode{constructor(data){this.data=data;this.left=null;this.right=null;}}//Driver Code Ends// Performs inorder traversal of BST// and stores values in sorted orderfunctioninorder(root,sortedInorder){if(root===null)return;// Traverse left subtreeinorder(root.left,sortedInorder);sortedInorder.push(root.data);// Traverse right subtreeinorder(root.right,sortedInorder);}// Returns the minimum value in a BSTfunctionminValue(root){if(root===null)return-1;constsortedInorder=[];// Get all BST values in sorted orderinorder(root,sortedInorder);returnsortedInorder[0];}//Driver Code Starts// Driver Code// Create BST// 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));//Driver Code Ends
Output
1
[Better Approach] Using Recursion - O(h) Time and O(h) Space
The idea is to start from the root and keep traversing the left child recursively (or iteratively) until a node has no left child. This node contains the minimum value in the BST.
C++
//Driver Code Starts#include<iostream>usingnamespacestd;// Node structureclassNode{public:intdata;Node*left,*right;Node(intx){data=x;left=right=nullptr;}};//Driver Code EndsintminValue(Node*root){// If left child is null, root is minimumif(root->left==nullptr)returnroot->data;// Recurse on left childreturnminValue(root->left);}//Driver Code Startsintmain(){// Create BST// 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)<<endl;}//Driver Code Ends
Java
//Driver Code StartsclassNode{intdata;Nodeleft,right;Node(intx){data=x;left=right=null;}}classGFG{//Driver Code EndsstaticintminValue(Noderoot){// If left child is null, root is minimumif(root.left==null)returnroot.data;// Recurse on left childreturnminValue(root.left);}//Driver Code Startspublicstaticvoidmain(String[]args){// Create BST// 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));}}//Driver Code Ends
Python
#Driver Code Starts# Node structureclassNode:def__init__(self,x):self.data=xself.left=Noneself.right=None#Driver Code EndsdefminValue(root):# If left child is null, root is minimumifroot.leftisNone:returnroot.data# Recurse on left childreturnminValue(root.left)#Driver Code Startsif__name__=="__main__":# Create BST# 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))#Driver Code Ends
C#
//Driver Code StartsusingSystem;// Node structureclassNode{publicintdata;publicNodeleft,right;publicNode(intx){data=x;left=right=null;}}classGFG{//Driver Code EndsstaticintminValue(Noderoot){// If left child is null, root is minimumif(root.left==null)returnroot.data;// Recurse on left childreturnminValue(root.left);}//Driver Code StartsstaticvoidMain(){// Create BST// 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));}}//Driver Code Ends
JavaScript
//Driver Code Starts// Node structureclassNode{constructor(x){this.data=x;this.left=null;this.right=null;}}//Driver Code EndsfunctionminValue(root){// If left child is null, root is minimumif(root.left===null)returnroot.data;// Recurse on left childreturnminValue(root.left);}//Driver Code Starts// Create BST// 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));//Driver Code Ends
Output
1
[Expected Approach] Using Iterative Way - 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.
C++
//Driver Code Starts#include<iostream>usingnamespacestd;// Node structurestructNode{intdata;Node*left,*right;Node(intval){data=val;left=right=nullptr;}};//Driver Code EndsintminValue(Node*root){if(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;}//Driver Code Startsintmain(){// 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)<<"";return0;}//Driver Code Ends
C
//Driver Code Starts#include<stdio.h>#include<stdlib.h>#include<limits.h>// Node structurestructNode{intdata;structNode*left;structNode*right;};structNode*createNode(intval){structNode*node=(structNode*)malloc(sizeof(structNode));node->data=val;node->left=node->right=NULL;returnnode;}//Driver Code EndsintminValue(structNode*root){if(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;}//Driver Code Startsintmain(){// 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",minValue(root));return0;}//Driver Code Ends
Java
//Driver Code Starts// Node structureclassNode{intdata;Nodeleft,right;Node(intval){data=val;left=right=null;}}classGFG{//Driver Code EndsstaticintminValue(Noderoot){if(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;}//Driver Code Startspublicstaticvoidmain(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));}}//Driver Code Ends
Python
#Driver Code Starts# Node structureclassNode:def__init__(self,data):self.data=dataself.left=Noneself.right=None#Driver Code EndsdefminValue(root):ifrootisNone: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.data#Driver Code Startsif__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))#Driver Code Ends
C#
//Driver Code StartsusingSystem;usingSystem.Collections.Generic;// Node structureclassNode{publicintdata;publicNodeleft,right;publicNode(intval){data=val;left=right=null;}}classGFG{//Driver Code EndsstaticintminValue(Noderoot){if(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;}//Driver Code StartsstaticvoidMain(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));}}//Driver Code Ends
JavaScript
//Driver Code Starts// Node structureclassNode{constructor(data){this.data=data;this.left=null;this.right=null;}}//Driver Code EndsfunctionminValue(root){if(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;}//Driver Code Starts// Driver Code// 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));//Driver Code Ends