-
Notifications
You must be signed in to change notification settings - Fork 47
/
Shortest Range In BST.cpp
67 lines (64 loc) · 2.01 KB
/
Shortest Range 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
62
63
64
65
66
67
private:
void storeInOrder(Node *root, int depth, vector<int> &inOrder,
vector<int> &level) {
if (!root) return;
storeInOrder(root->left, depth + 1, inOrder, level);
inOrder.push_back(root->data);
level.push_back(depth);
storeInOrder(root->right, depth + 1, inOrder, level);
}
public:
pair<int, int> shortestRange(Node *root) {
vector<int> inOrder, level;
storeInOrder(root, 0, inOrder, level);
int i = 0, j = 0, k = 0, cntZero = 0,
maxDepth = *max_element(level.begin(), level.end()) + 1;
vector<int> depth(maxDepth, 0);
for (k = 0; k < level.size(); k++) {
depth[level[k]]++;
if (level[k] == 0) {
j = k;
break;
}
}
for (int u : depth) {
if (u == 0) {
cntZero++;
}
}
int x = *inOrder.begin(), y = inOrder.back();
if (cntZero == 0) {
x = inOrder[i], y = inOrder[j];
}
while (i <= k && j < inOrder.size()) {
while (j < inOrder.size()) {
if (cntZero == 0) {
if ((y - x) > (inOrder[j] - inOrder[i])) {
x = inOrder[i];
y = inOrder[j];
}
break;
}
j++;
if (j >= inOrder.size()) {
break;
}
if (depth[level[j]] == 0) {
cntZero--;
}
depth[level[j]]++;
}
while (!cntZero && i <= k) {
if ((y - x) > (inOrder[j] - inOrder[i])) {
x = inOrder[i];
y = inOrder[j];
}
depth[level[i]]--;
if (depth[level[i]] == 0) {
cntZero++;
}
i++;
}
}
return {x, y};
}