-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay42.cpp
More file actions
103 lines (87 loc) · 2.02 KB
/
Day42.cpp
File metadata and controls
103 lines (87 loc) · 2.02 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
This problem was asked by Oracle.
Given a binary search tree, find the floor and ceiling of a given integer. The floor is the highest element in the tree less than or equal to an integer, while the ceiling is the lowest element in the tree greater than or equal to an integer.
If either value does not exist, return None.
*/
#include <iostream>
using namespace std;
struct TreeNode
{
int val;
TreeNode *left, *right;
TreeNode(int v) : val(v), left(nullptr), right(nullptr) {}
};
class Solution
{
public:
int floor = -1, ceil = -1;
void findFloor(TreeNode *node, int x)
{
if (!node)
return;
if (node->val == x)
{
floor = x;
return;
}
if (node->val > x)
{
findFloor(node->left, x);
}
else
{
floor = node->val;
findFloor(node->right, x);
}
}
void findCeil(TreeNode *node, int x)
{
if (!node)
return;
if (node->val == x)
{
ceil = x;
return;
}
if (node->val < x)
{
findCeil(node->right, x);
}
else
{
ceil = node->val;
findCeil(node->left, x);
}
}
pair<int, int> floorCeilBST(TreeNode *root, int x)
{
floor = ceil = -1;
findFloor(root, x);
findCeil(root, x);
return {floor, ceil};
}
};
int main()
{
/*
8
/ \
4 12
/ \ \
2 6 14
*/
TreeNode *root = new TreeNode(8);
root->left = new TreeNode(4);
root->right = new TreeNode(12);
root->left->left = new TreeNode(2);
root->left->right = new TreeNode(6);
root->right->right = new TreeNode(14);
Solution sol;
// Test cases
for (int x : {1, 5, 8, 10, 15})
{
auto [floor, ceil] = sol.floorCeilBST(root, x);
cout << "x = " << x << " → Floor: " << floor << ", Ceil: " << ceil << endl;
}
return 0;
}