-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMinimum_element_in_BST.cpp
52 lines (44 loc) · 1.01 KB
/
Minimum_element_in_BST.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
Problem Statement:
-----------------
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).
*/
// Link --> https://practice.geeksforgeeks.org/problems/minimum-element-in-bst/1#
// Code:
int minValue(Node* root)
{
if(root == NULL)
return 0;
if(root->left == NULL)
return root->data;
else
{
while(root->left)
root= root->left;
return root->data;
}
}