-
Notifications
You must be signed in to change notification settings - Fork 0
/
findPathInBSTSumToNum.cc
68 lines (61 loc) · 1.6 KB
/
findPathInBSTSumToNum.cc
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
#include <iostream>
#include <vector>
using namespace std;
typedef struct elementT {
struct elementT *left;
struct elementT *right;
int data;
} element;
void findPathInBSTSumToNumSub(element *ele, vector<int> *v, int *sum, int num) {
if(!ele)
return;
if(*sum == num) {
vector<int>::iterator it = (*v).begin();
for(; it != (*v).end(); it++)
cout << *it << " ";
cout << endl;
return;
}
if(ele->left) {
(*v).push_back(ele->left->data);
(*sum) += ele->left->data;
findPathInBSTSumToNumSub(ele->left, v, sum, num);
(*sum) -= ele->left->data;
(*v).pop_back();
}
if(ele->right) {
(*v).push_back(ele->right->data);
(*sum) += ele->right->data;
findPathInBSTSumToNumSub(ele->right, v, sum, num);
(*sum) -= ele->right->data;
(*v).pop_back();
}
}
void findPathInBSTSumToNum(element *ele, int num) {
vector<int> v;
int sum = 0;
if(ele) {
sum += ele->data;
v.push_back(ele->data);
findPathInBSTSumToNumSub(ele, &v, &sum, num);
}
}
element *makeNode(int d) {
element *ele = new element();
ele->data = d;
return ele;
}
main()
{
element *root = makeNode(50);
root->left = makeNode(25);
root->right = makeNode(75);
root->left->left = makeNode(12);
root->left->right = makeNode(40);
root->right->left = makeNode(65);
root->right->right = makeNode(90);
findPathInBSTSumToNum(root, 50);
findPathInBSTSumToNum(root, 87);
findPathInBSTSumToNum(root, 190);
findPathInBSTSumToNum(root, 1);
}