Minimum element in BST | Practice | GeeksforGeeks

Given a Binary Search Tree. The task is to find the minimum element in this given BST. 
Example 1:
Input:
           5
         /    \
        4      6
       /        \
      3          7
     /
    1
Output: 1

Example 2:
Input:
             9
              \
               10
                \
                 11
Output: 9

Your Task:
The task is to complete the function minValue() which takes root as the argument and returns the minimum element of BST. If the tree is empty, there is no minimum elemnt, so retutn -1 in that case.
Expected Time Complexity: O(Height of the BST)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
0 <= N <= 104
0
sikkusaurav1236 days ago
using inorder traversal : 
vector<int>v;void solve(Node *root){       if(root!=NULL)   {       solve(root->left);       v.push_back(root->data);       return;   }}int minValue(Node* root) {   // Code here    if(root==NULL)   {       return -1;   }   v.clear();   solve(root);   return v[0];}
0
deepeshupadhyay853 weeks ago
Easy to memorisze
class Tree {   // Function to find the minimum element in the given BST.   int minValue(Node node) {       if(node == null) return -1;       if(node.left!=null){           return minValue(node.left);       }else{           return node.data;       }   }} 
0
00thirt13n3 weeks ago
Easy C++ Solution:
int minValue(Node* root) {
    if(!root) return -1;
    if(!root->left) return root->data;
    return minValue(root->left);
}
0
jaydhurat3 weeks ago
class Tree {
   // Function to find the minimum element in the given BST.
   int minValue(Node root) {
       if(root==null)
       {
           return -1;
       }
       if(root.left==null)
       {
           return root.data;
       }
       int min=root.data;
       while(root.left!=null)
       {
           min=root.left.data;
           root=root.left;
       }
       return min;
   }
}

0
rajpateriya3 weeks ago
JAVA solution
     if(root==null)
         return -1;
         
     while(root.left != null)
         root=root.left;
     return root.data;
0
shivanshchandra23 weeks ago
CPP code
 
int minValue(Node* root) {   // Code here   if(root == NULL){       return -1;   }   if(root->left == NULL){       return root->data;   }   return minValue(root->left);}
0
kritikasinha2564 weeks ago
PYTHON SOLUTION
def minValue(root):   if root is None:       return -1   elif root.left is not None:       return minValue(root.left)   else:       return root.data
0
durgeshagrharibst12341 month ago
int minValue(Node* root) {   if(root==NULL)       return -1;   if(root->left==NULL)       return root->data;   else       minValue(root->left);}
0
prabhakarsati294261 month ago
#User function Template for python3
#Function to find the minimum element in the given BST.def minValue(root):   ##Your code here   if root is None:       return -1   curr = root   while(curr.left !=None):       curr = curr.left   return curr.data           
#{ #  Driver Code Starts#Initial Template for Python 3
from collections import deque# Tree Nodeclass Node:   def __init__(self, val):       self.right = None       self.data = val       self.left = None
# Function to Build Tree   def buildTree(s):   #Corner Case   if(len(s)==0 or s[0]=="N"):                  return None          # Creating list of strings from input    # string after spliting by space   ip=list(map(str,s.split()))      # Create the root of the tree   root=Node(int(ip[0]))                        size=0   q=deque()      # Push the root to the queue   q.append(root)                               size=size+1       # Starting from the second element   i=1                                          while(size>0 and i<len(ip)):       # Get and remove the front of the queue       currNode=q[0]       q.popleft()       size=size-1              # Get the current node's value from the string       currVal=ip[i]              # If the left child is not null       if(currVal!="N"):                      # Create the left child for the current node           currNode.left=Node(int(currVal))                      # Push it to the queue           q.append(currNode.left)           size=size+1       # For the right child       i=i+1       if(i>=len(ip)):           break       currVal=ip[i]              # If the right child is not null       if(currVal!="N"):                      # Create the right child for the current node           currNode.right=Node(int(currVal))                      # Push it to the queue           q.append(currNode.right)           size=size+1       i=i+1   return root      if __name__=="__main__":   t=int(input())   for _ in range(0,t):       s=input()       root=buildTree(s)       print(minValue(root))# } Driver Code Ends
0
ramyan18141 month ago
//c++ solution
 
int minValue(Node* root) {   // Code here      Node* temp=root;      if(root==NULL) return -1;     while(temp->left!=NULL){       temp=temp->left;   }   return temp->data; }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.