-
Notifications
You must be signed in to change notification settings - Fork 0
/
#135.cpp
73 lines (61 loc) · 1.41 KB
/
#135.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
68
69
70
71
72
73
#include<iostream>
#include<vector>
#include<climits>
using namespace std;
struct Node {
int val;
Node* left;
Node* right;
Node(int i) {
val = i;
left = nullptr;
right = nullptr;
}
};
int minimum;
vector<int> min_path;
void printBT(const string& prefix, const Node* node, bool isLeft) {
if (node != nullptr) {
cout << prefix << (isLeft ? "├──" : "└──" ) << node->val << endl;
printBT(prefix + (isLeft ? "│ " : " "), node->left, true);
printBT(prefix + (isLeft ? "│ " : " "), node->right, false);
}
}
void recurse(Node* curr, int sum, vector<int> path) {
sum += curr->val;
path.push_back(curr->val);
if (curr->left == nullptr && curr->right == nullptr) {
if (sum < minimum) {
minimum = sum;
min_path = path;
}
return;
}
if (curr->left != nullptr) recurse(curr->left, sum, path);
if (curr->right != nullptr) recurse(curr->right, sum, path);
}
void get_minimum_path(Node* root) {
minimum = INT_MAX;
vector<int> temp;
recurse(root, 0, temp);
}
int main() {
Node* n10 = new Node(10);
Node* n5_1 = new Node(5);
Node* n5_2 = new Node(5);
Node* n2 = new Node(2);
Node* n1 = new Node(1);
Node* n_1 = new Node(-1);
n10->left = n5_1;
n5_1->right = n2;
n10->right = n5_2;
n5_2->right = n1;
n1->left = n_1;
printBT("", n10, false);
get_minimum_path(n10);
cout << minimum << endl;
for (int i : min_path) {
cout << i << ",";
}
cout << endl;
}