-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFloor in BST
43 lines (35 loc) · 837 Bytes
/
Floor in BST
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
#include <bits/stdc++.h>
/************************************************************
Following is the TreeNode class structure
template <typename T>
class TreeNode {
public:
T val;
TreeNode<T> *left;
TreeNode<T> *right;
TreeNode(T val) {
this->val = val;
left = NULL;
right = NULL;
}
};
************************************************************/
int floorInBST(TreeNode<int> * root, int X)
{
// Write your code here.
int floor=-1;
while(root){
if(root->val == X){
floor=root->val;
return floor;
}
if(X > root->val){
floor=root->val;
root=root->right;
}
else{
root=root->left;
}
}
return floor;
}