Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on Tree
  • Practice Tree
  • MCQs on Tree
  • Tutorial on Tree
  • Types of Trees
  • Basic operations
  • Tree Traversal
  • Binary Tree
  • Complete Binary Tree
  • Ternary Tree
  • Binary Search Tree
  • Red-Black Tree
  • AVL Tree
  • Full Binary Tree
  • B-Tree
  • Advantages & Disadvantages
Open In App
Next Article:
Check if an array represents Inorder of Binary Search tree or not
Next article icon

Minimum in a Binary Search Tree

Last Updated : 29 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

Given the root of a Binary Search Tree. The task is to find the minimum valued element in this given BST.

Example: 

Input:

ex-1

Output: 1
Explanation: The minimum element in the given BST is 1.

Input:

ex-2

Output: 2
Explanation: The minimum element in the given BST is 2

Table of Content

  • [Naive Approach] Using Inorder Traversal – O(n) Time and O(n) Space
  • [Alternate Approach] Using Recursion- O(n) Time and O(n) Space
  • [Expected Approach] Traversing Only Left Edges – O(h) Time and O(1) Space

[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> using namespace std;  struct Node {     int data;     Node *left, *right;      Node(int val) {         data = val;         left = right = nullptr;     } };  // Recursive function to solve and store elements  // in a vector void inorder(Node* root, vector<int>& sortedInorder) {        // Base Case     if (root == nullptr) return;      // Traverse left subtree     inorder(root->left, sortedInorder);      // Store the current node's data     sortedInorder.push_back(root->data);      // Traverse right subtree     inorder(root->right, sortedInorder); }  // Function to find the minimum value in BST int minValue(Node* root) {     if (root == nullptr) {         return -1;     }          vector<int> sortedInorder;          // Call the recursive inorder function     inorder(root, sortedInorder);          // Return the first element, which is the minimum     return sortedInorder[0]; }  int main() {      // Representation of input binary search tree     //        5     //       / \     //      4   6     //     /     \     //    3       7     //   /      //  1     Node* root = new Node(5);     root->left = new Node(4);     root->right = new Node(6);     root->left->left = new Node(3);     root->right->right = new Node(7);     root->left->left->left = new Node(1);      cout << minValue(root) << "\n";      return 0; } 
C
// C code to find minimum value in BST // using inorder traversal #include <stdio.h> #include <stdlib.h> #include <limits.h>  struct Node {     int data;     struct Node* left;     struct Node* right; };  // Recursive function to traverse the tree  // and store elements in a vector void inorder(struct Node* root, int *sortedInorder,                                            int *index) {        // Base Case     if (root == NULL) return;      // Traverse left subtree     inorder(root->left, sortedInorder, index);      // Store the current node's data     sortedInorder[(*index)++] = root->data;      // Traverse right subtree     inorder(root->right, sortedInorder, index); }  // Function to find the minimum value in a BST int minValue(struct Node* root) {     if (root == NULL) {         return -1;     }          // Create an array to hold inorder elements     int sortedInorder[20000];       int index = 0;          // Call the recursive inorder function     inorder(root, sortedInorder, &index);          // Return the first element, which is the minimum     return sortedInorder[0]; }  struct Node* createNode(int val) {     struct Node* node       = (struct Node*)malloc(sizeof(struct Node));     node->data = val;     node->left = node->right = NULL;     return node; }  int main() {      // Representation of input binary search tree     //        5     //       / \     //      4   6     //     /     \     //    3       7     //   /     //  1     struct Node* 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));      return 0; } 
Java
// Java code to find minimum value in BST // using inorder traversal import java.util.ArrayList;  class Node {     int data;     Node left, right;      Node(int val) {         data = val;         left = right = null;     } }  class GfG {      static void inorder(Node root,                       ArrayList<Integer> sortedInorder) {                // Base Case         if (root == null) return;          // Traverse left subtree         inorder(root.left, sortedInorder);          // Store the current node's data         sortedInorder.add(root.data);          // Traverse right subtree         inorder(root.right, sortedInorder);     }      static int minValue(Node root) {                if (root == null) {             return -1;         }          // Create an ArrayList to hold inorder elements         ArrayList<Integer> sortedInorder                                   = new ArrayList<>();          // Call the recursive inorder function         inorder(root, sortedInorder);          // Return the first element, which is the minimum         return sortedInorder.get(0);     }      public static void main(String[] args) {          // Representation of input binary search tree         //        5         //       / \         //      4   6         //     /     \         //    3       7         //   /         //  1         Node root = new Node(5);         root.left = new Node(4);         root.right = new Node(6);         root.left.left = new Node(3);         root.right.right = new Node(7);         root.left.left.left = new Node(1);          System.out.println(minValue(root));     } } 
Python
# Python code to find minimum value in BST # using inorder traversal  class Node:     def __init__(self, data):         self.data = data         self.left = None         self.right = None  def inorder(root, sorted_inorder):        # Base Case     if root is None:         return      # Traverse left subtree     inorder(root.left, sorted_inorder)      # Store the current node's data     sorted_inorder.append(root.data)      # Traverse right subtree     inorder(root.right, sorted_inorder)  def minValue(root):     if root is None:         return -1      # Using a list to hold inorder elements     sorted_inorder = []        # Call the recursive inorder function     inorder(root, sorted_inorder)      # Return the first element, which is the minimum     return sorted_inorder[0]  if __name__ == "__main__":        # Representation of input binary search tree     #        5     #       / \     #      4   6     #     /     \     #    3       7     #   /     #  1     root = 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 traversal using System; using System.Collections.Generic;  class Node {     public int data;     public Node left, right;      public Node(int val) {         data = val;         left = right = null;     } }  class GfG {      static void Inorder(Node root, List<int> sortedInorder) {                // Base Case         if (root == null) return;          // Traverse left subtree         Inorder(root.left, sortedInorder);          // Store the current node's data         sortedInorder.Add(root.data);          // Traverse right subtree         Inorder(root.right, sortedInorder);     }      static int MinValue(Node root) {         if (root == null) {             return -1;         }          // Create a list to hold inorder elements         List<int> sortedInorder = new List<int>();          // Call the recursive inorder function         Inorder(root, sortedInorder);          // Return the first element, which is the minimum         return sortedInorder[0];     }      static void Main(string[] args) {          // Representation of input binary search tree         //        5         //       / \         //      4   6         //     /     \         //    3       7         //   /         //  1         Node root = new Node(5);         root.left = new Node(4);         root.right = new Node(6);         root.left.left = new Node(3);         root.right.right = new Node(7);         root.left.left.left = new Node(1);          Console.WriteLine(MinValue(root));     } } 
JavaScript
// JavaScript code to find minimum value in BST // using inorder traversal  class Node {     constructor(data) {         this.data = data;         this.left = null;         this.right = null;     } }  function inorder(root, sortedInorder) {        // Base case     if (root === null) return;      // Traverse left subtree     inorder(root.left, sortedInorder);      // Store the current node's data     sortedInorder.push(root.data);      // Traverse right subtree     inorder(root.right, sortedInorder); }  function minValue(root) {     if (root === null) {         return -1;     }      // Create an array to hold inorder elements     let sortedInorder = [];       // Call the recursive inorder function     inorder(root, sortedInorder);      // Return the first element, which is the minimum     return sortedInorder[0]; }  // Representation of input binary search tree //        5 //       / \ //      4   6 //     /     \ //    3       7 //   / //  1 let root = new Node(5); root.left = new Node(4); root.right = new Node(6); root.left.left = new Node(3); root.right.right = new Node(7); root.left.left.left = new Node(1);  console.log(minValue(root)); 

Output
1 

Time Complexity: O(n), since we traversed through all the elements in a BST.
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

The idea is to 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. Please refer to Find the node with minimum value in a Binary Search Tree using recursion for implementation.

[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> using namespace std;  struct Node {     int data;     Node *left, *right;      Node(int val) {         data = val;         left = right = nullptr;     } };  int minValue(Node* root) {        // base case     if (root == nullptr) {         return -1;     }      Node* curr = root;      // leftmost node is minimum so we move in BST till     // left node is not nullptr     while (curr->left != nullptr) {         curr = curr->left;     }      // returning the data at the leftmost node     return curr->data; }  int main() {      // Representation of input binary search tree     //        5     //       / \     //      4   6     //     /     \     //    3       7     //   /      //  1     Node* root = new Node(5);     root->left = new Node(4);     root->right = new Node(6);     root->left->left = new Node(3);     root->right->right = new Node(7);     root->left->left->left = new Node(1);      cout << minValue(root) << "\n";      return 0; } 
C
// C code to find minimum value in BST // using iteration #include <stdio.h> #include <stdlib.h> #include <limits.h>  struct Node {     int data;     struct Node* left;     struct Node* right; };  // Function to find minimum value in BST int minValue(struct Node* root) {        // base case     if (root == NULL) {         return -1;     }      struct Node* curr = root;      // leftmost node is minimum, so move      // till left is not NULL     while (curr->left != NULL) {         curr = curr->left;     }      // returning the data at the leftmost node     return curr->data; }  struct Node* createNode(int val) {     struct Node* node              = (struct Node*)malloc(sizeof(struct Node));     node->data = val;     node->left = node->right = NULL;     return node; }  int main() {      // Representation of input binary search tree     //        5     //       / \     //      4   6     //     /     \     //    3       7     //   /     //  1     struct Node* 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));      return 0; } 
Java
// Java code to find minimum value in BST // using iteration class Node {     int data;     Node left, right;      Node(int val) {         data = val;         left = right = null;     } }  public class GfG {      public static int minValue(Node root) {               // base case         if (root == null) {             return -1;         }          Node curr = root;          // leftmost node is minimum, so move till          // left is not null         while (curr.left != null) {             curr = curr.left;         }          // returning the data at the leftmost node         return curr.data;     }      public static void main(String[] args) {          // Representation of input binary search tree         //        5         //       / \         //      4   6         //     /     \         //    3       7         //   /         //  1         Node root = new Node(5);         root.left = new Node(4);         root.right = new Node(6);         root.left.left = new Node(3);         root.right.right = new Node(7);         root.left.left.left = new Node(1);          System.out.println(minValue(root));     } } 
Python
# Python code to find minimum value in BST # using using iterationl class Node:     def __init__(self, data):         self.data = data         self.left = None         self.right = None   # Function to find the minimum value in BST def minValue(root):     # base case     if root is None:         return -1      curr = root      # leftmost node is minimum, so move      # till left is not None     while curr.left is not None:         curr = curr.left      # returning the data at the leftmost node     return curr.data  if __name__ == "__main__":        # Representation of input binary search tree     #        5     #       / \     #      4   6     #     /     \     #    3       7     #   /     #  1     root = 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 iteration using System; using System.Collections.Generic;  class Node {     public int data;     public Node left, right;      public Node(int val) {         data = val;         left = right = null;     } }  class GfG {      static int minValue(Node root) {                // base case         if (root == null) {             return -1;         }          Node curr = root;          // leftmost node is minimum, so move          // till left is not null         while (curr.left != null) {             curr = curr.left;         }          // returning the data at the leftmost node         return curr.data;     }      static void Main(string[] args) {          // Representation of input binary search tree         //        5         //       / \         //      4   6         //     /     \         //    3       7         //   /         //  1         Node root = new Node(5);         root.left = new Node(4);         root.right = new Node(6);         root.left.left = new Node(3);         root.right.right = new Node(7);         root.left.left.left = new Node(1);          Console.WriteLine(minValue(root));     } } 
JavaScript
// Javascript code to find minimum value in BST // using iteration class Node {     constructor(data) {         this.data = data;         this.left = null;         this.right = null;     } }  // Function to find the minimum value in BST function minValue(root) {      // base case     if (root === null) {         return -1;     }      let curr = root;      // leftmost node is minimum, so move till      // left is not null     while (curr.left !== null) {         curr = curr.left;     }      // returning the data at the leftmost node     return curr.data; }  // Representation of input binary search tree //        5 //       / \ //      4   6 //     /     \ //    3       7 //   / //  1 let root = new Node(5); root.left = new Node(4); root.right = new Node(6); root.left.left = new Node(3); root.right.right = new Node(7); root.left.left.left = new Node(1);  // Output the minimum value in the BST console.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.



Next Article
Check if an array represents Inorder of Binary Search tree or not
author
kartik
Improve
Article Tags :
  • Binary Search Tree
  • DSA
  • Tree
  • Microsoft
Practice Tags :
  • Microsoft
  • Binary Search Tree
  • Tree

Similar Reads

  • Binary Search Tree
    A Binary Search Tree (or BST) is a data structure used in computer science for organizing and storing data in a sorted manner. Each node in a Binary Search Tree has at most two children, a left child and a right child, with the left child containing values less than the parent node and the right chi
    3 min read
  • Introduction to Binary Search Tree
    Binary Search Tree is a data structure used in computer science for organizing and storing data in a sorted manner. Binary search tree follows all properties of binary tree and for every nodes, its left subtree contains values less than the node and the right subtree contains values greater than the
    3 min read
  • Applications of BST
    Binary Search Tree (BST) is a data structure that is commonly used to implement efficient searching, insertion, and deletion operations along with maintaining sorted sequence of data. Please remember the following properties of BSTs before moving forward. The left subtree of a node contains only nod
    2 min read
  • Applications, Advantages and Disadvantages of Binary Search Tree
    A Binary Search Tree (BST) is a data structure used to storing data in a sorted manner. Each node in a Binary Search Tree has at most two children, a left child and a right child, with the left child containing values less than the parent node and the right child containing values greater than the p
    2 min read
  • Insertion in Binary Search Tree (BST)
    Given a BST, the task is to insert a new node in this BST. Example: How to Insert a value in a Binary Search Tree:A new key is always inserted at the leaf by maintaining the property of the binary search tree. We start searching for a key from the root until we hit a leaf node. Once a leaf node is f
    15+ min read
  • Searching in Binary Search Tree (BST)
    Given a BST, the task is to search a node in this BST. For searching a value in BST, consider it as a sorted array. Now we can easily perform search operation in BST using Binary Search Algorithm. Input: Root of the below BST Output: TrueExplanation: 8 is present in the BST as right child of rootInp
    7 min read
  • Deletion in Binary Search Tree (BST)
    Given a BST, the task is to delete a node in this BST, which can be broken down into 3 scenarios: Case 1. Delete a Leaf Node in BST Case 2. Delete a Node with Single Child in BST Deleting a single child node is also simple in BST. Copy the child to the node and delete the node. Case 3. Delete a Node
    10 min read
  • Binary Search Tree (BST) Traversals – Inorder, Preorder, Post Order
    Given a Binary Search Tree, The task is to print the elements in inorder, preorder, and postorder traversal of the Binary Search Tree.  Input:  Output: Inorder Traversal: 10 20 30 100 150 200 300Preorder Traversal: 100 20 10 30 200 150 300Postorder Traversal: 10 30 20 150 300 200 100 Input:  Output:
    11 min read
  • Balance a Binary Search Tree
    Given a BST (Binary Search Tree) that may be unbalanced, the task is to convert it into a balanced BST that has the minimum possible height. Examples: Input: Output: Explanation: The above unbalanced BST is converted to balanced with the minimum possible height. Input: Output: Explanation: The above
    10 min read
  • Self-Balancing Binary Search Trees
    Self-Balancing Binary Search Trees are height-balanced binary search trees that automatically keep the height as small as possible when insertion and deletion operations are performed on the tree. The height is typically maintained in order of logN so that all operations take O(logN) time on average
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences