-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathfind-closest-in-bst.cpp
61 lines (54 loc) · 1.5 KB
/
find-closest-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
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <cmath>
#include <float.h>
using namespace std;
class BST {
public:
int value;
BST* left;
BST* right;
BST(int val);
BST& insert(int val);
};
int findClosestValueInBst(BST* tree, int target);
int findClosestValueInBstHelper(BST* tree, int target, double closest);
int findClosestValueInBst(BST* tree, int target) {
// Write your code here.
return findClosestValueInBstHelper(tree, target, DBL_MAX);
}
// Recursive
// Average: O(log n) time | O(log n) space
// Worst: O(n) time | O(n) space
int findClosestValueInBstHelper(BST* tree, int target, double closest) {
if (abs(closest - target) > abs(tree->value - target)) {
closest = tree->value;
if (target == tree->value) { // Early stop if target equal to current value
return (int) closest;
}
}
if (target < tree->value && tree->left != NULL) {
return findClosestValueInBstHelper(tree->left, target, closest);
} else if (target > tree->value && tree->right != NULL) {
return findClosestValueInBstHelper(tree->right, target, closest);
} else {
return (int) closest;
}
}
// Iterative
// Average: O(log n) time | O(1) space
// Worst: O(n) time | O(1) space
int findClosestValueInBstHelper(BST* tree, int target, double closest) {
while (tree != NULL) {
if (abs(closest - target) > abs(tree->value - target)) {
closest = tree->value;
}
if (target < tree->value) {
tree = tree->left;
} else if (target > tree->value) {
tree = tree->right;
} else {
break;
}
}
return (int) closest;
}