-
Notifications
You must be signed in to change notification settings - Fork 57
/
Print_path_IN_range.cpp
57 lines (56 loc) · 1.1 KB
/
Print_path_IN_range.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
#include<iostream>
#include<string>
using namespace std;
class node
{
public:
int data;
node* right;
node* left;
node(int d)
{
data = d;
left = NULL;
right = NULL;
}
};
node* buildtree()
{
int d;
cin>>d;
if(d == -1)
{
return NULL;
}
node* root = new node(d);
root->left = buildtree();
root->right = buildtree();
return root;
}
void pathtoleaffromnode(node* root, string path, int sum, int lo, int ui)
{
if(root == NULL)
{
return;
}
string s;
if(root->left == NULL and root->right == NULL)
{
sum = sum + root->data;
if(sum <= ui && sum >= lo)
{
s = path + to_string(root->data);
cout<<s;
}
return;
}
pathtoleaffromnode(root->left,s +" " , sum+root->data,lo,ui);
pathtoleaffromnode(root->right,s +" " , sum+root->data,lo,ui);
}
int main()
{
node* root = buildtree();
int lo = 100;
int ui = 250;
pathtoleaffromnode(root,"",0,lo,ui);
}