-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFloor in BST.cpp
28 lines (27 loc) · 929 Bytes
/
Floor 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
Floor in BST
You are given a BST(Binary Search Tree) with n number of nodes and value x. your task is to find the greatest value node of the BST which is smaller than or equal to x.
Note: when x is smaller than the smallest node of BST then returns -1.
link-->>>> https://practice.geeksforgeeks.org/problems/floor-in-bst/1
class Solution
{
public:
int floor(Node *root, int x)
{
if (!root)
return -1;
if (root->data == x)
return x;
int maxi = INT_MIN;
if (root->data <= x)
maxi = root->data;
if (root->left)
{
maxi = max(maxi, floor(root->left, x));
}
if (root->right)
{
maxi = max(maxi, floor(root->right, x));
}
return maxi != INT_MIN ? maxi : -1;
}
};