Find the node with minimum value in a Binary Search Tree using recursion
Last Updated :
24 Sep, 2024
Given the root of a Binary Search Tree, the task is to find the minimum valued element in this given BST.
Examples:
Input:
Output: 1
Explanation: The minimum element in the given BST is 1.
Input:
Output: 2
Explanation: The minimum element in the given BST is 2.
Approach:
The idea is to recursively traverse the Binary Search Tree (BST) starting from the root node, moving to the left child until a node with no left child (i.e., a left NULL pointer) is reached. In a BST, all values in the left subtree of a node are less than the node's value, which means that the leftmost node holds the smallest value in the entire tree. Thus, when we find a node where the left child is NULL, that node is guranteed to contain the minimum value. This approach efficiently locates the minimum value without needing to explore unnecessary parts of the tree.
Below is the implementation of the above approach:
C++
// C++ code to find minimum value in BST
// using recursion
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *left, *right;
Node(int val) {
data = val;
left = right = nullptr;
}
};
// Function to find the minimum value in a BST
int minValue(Node* root) {
// If the root is null or left child is null,
// return the current node's value
if (root == nullptr || root->left == nullptr) {
return root->data;
}
// Recursively find the minimum value in the left subtree
return minValue(root->left);
}
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 recursion
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
struct Node {
int data;
struct Node *left, *right;
};
// Function to find the minimum value in a BST
int minValue(struct Node* root) {
// If root is NULL or left child is NULL,
// return root data
if (root == NULL || root->left == NULL) {
return root->data;
}
// Recursively get the minimum value
// from the left subtree
return minValue(root->left);
}
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 recursion
class Node {
int data;
Node left, right;
Node(int val) {
data = val;
left = right = null;
}
}
class GfG {
// If root is null or left is null, return root data
static int minValue(Node root) {
// If the root is null or left child is null,
// return the current node's value
if (root == null || root.left == null) {
return root.data;
}
// Recursively get min value from the left subtree
return minValue(root.left);
}
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 recursion
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# Function to find the minimum value in a BST
def minValue(root):
# If root is None or left is None, return root data
if root is None or root.left is None:
return root.data
# Recursively get the minimum value from
# the left subtree
return minValue(root.left)
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 recursion
using System;
class Node {
public int Data;
public Node left, right;
public Node(int data) {
this.Data = data;
left = right = null;
}
}
class GfG {
// Function to find the minimum value in the BST
static int MinValue(Node root) {
// If root is null or left is null, return
// root data
if (root == null || root.left == null) {
return root.Data;
}
// Recursively get the minimum value from
// the left subtree
return MinValue(root.left);
}
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 recursion
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
// Function to find the minimum value in the BST
function minValue(root) {
// If root is null or left is null, return root data
if (root === null || root.left === null) {
return root.data;
}
// Recursively get the minimum value from
// the left subtree
return minValue(root.left);
}
// 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));
Time Complexity: O(h) where h is the height of Binary Search Tree, This is because in the worst case, the function traverses from the root to the leftmost node.
Auxiliary Space: O(h), where h is the height of the tree due to the recursive call stack. In the worst case (unbalanced tree), this can be O(n).
Related article:
Similar Reads
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Dijkstra's Algorithm to find Shortest Paths from a Source to all Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example
12 min read
Selection Sort Selection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire array is sorted.First we find the smallest element an
8 min read