-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshortest_path_nodes.cpp
More file actions
78 lines (60 loc) · 1.69 KB
/
shortest_path_nodes.cpp
File metadata and controls
78 lines (60 loc) · 1.69 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
#include <iostream>
#include <vector>
#include <climits>
struct Node{
int value;
Node* left;;
Node* right;
};
void min_BST(Node* root, int& min_dist) {
if (!root or ( !root->left and !root->right )) return;
if (root->left and root->right) {
auto m = std::min(root->value-root->left->value,
root->right->value - root->value);
min_dist = std::min(m, min_dist);
min_BST(root->left, min_dist);
min_BST(root->right, min_dist);
} else if (root->left){
min_dist = std::min(min_dist,
root->value - root->left->value);
min_BST(root->left, min_dist);
} else if (root->right) {
min_dist = std::min(min_dist,
root->right->value - root->value);
min_BST(root->right, min_dist);
}
}
void collect(Node* root, std::vector<int>& values) {
if (!root) return;
collect(root->left, values);
values.push_back(root->value);
collect(root->right, values);
}
int minBST(Node* root){
int min_dist = INT_MAX;
std::vector<int> values;
collect(root, values);
for(auto i = 1; i < values.size(); ++i) {
min_dist = std::min(min_dist,
values[i] - values[i-1]);
}
return min_dist;
}
int main() {
Node root = {5};
Node three = {3};
root.left = &three;
Node two = {2};
root.left->left = &two;
Node four = {4};
root.left->left = &four;
Node seven = {7};
root.right = &seven;
Node six = {6};
root.right->left = &six;
std::cout << std::abs(minBST(&root)) << std::endl;
auto min_dist = INT_MAX;
min_BST(&root, min_dist);
std::cout << min_dist << std::endl;
return 0;
}