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
Find the node with maximum value in a Binary Search Tree using recursion Given a Binary Search Tree, the task is to find the node with maximum value. Examples: Input: Output: 22 Approach: Just traverse the node from root to right recursively until right is NULL. The node whose right is NULL is the node with the maximum value. Below is the implementation of the above appr
7 min read
Find the minimum Sub-tree with target sum in a Binary search tree Given a binary tree and a target, find the number of nodes in the minimum sub-tree with the given sum equal to the target which is also a binary search tree. Examples: Input: 13 / \ 5 23 / \ / \ N 17 N N / 16Target: 38Output: 3Explanation: 5, 17, 16 is the smallest subtree with length 3. Input: 7 /
9 min read
Leaf nodes from Preorder of a Binary Search Tree (Using Recursion) Given Preorder traversal of a Binary Search Tree. Then the task is to print leaf nodes of the Binary Search Tree from the given preorder.Examples : Input : preorder[] = {890, 325, 290, 530, 965};Output : 290 530 965Explanation: Below is the representation of BST using preorder array. Approach:To ide
8 min read
Find the parent of a node in the given binary tree Given a Binary Tree and a node, the task is to find the parent of the given node in the tree. Return -1 if the given node is the root node.Note: In a binary tree, a parent node of a given node is the node that is directly connected above the given node. Examples: Input: target = 3 Output: 1Explanati
6 min read
Find parent of given node in a Binary Tree with given postorder traversal Given two integers N and K where N denotes the height of a binary tree, the task is to find the parent of the node with value K in a binary tree whose postorder traversal is first 2^{N}-1 natural numbers (1, 2, ... 2^{N}-1) For N = 3, the Tree will be - 7 / \ 3 6 / \ / \ 1 2 4 5 Examples: Input: N =
9 min read