-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuva-112.cpp
101 lines (84 loc) · 2.07 KB
/
uva-112.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//uva 112
//Tree Summing
#include <iostream>
#include <string>
#include <stack>
using namespace std;
typedef struct node{
int value;
node * left;
node * right;
}node;
bool check_for_sum(node * tree, int sum);
node * build_tree(string input, int & index);
int main(void)
{
int n;
while(cin >> n){
string input;
stack <char> string_validness;
do{
char c;
cin >> c;
if(c == '(')
string_validness.push('(');
if(c == ')')
string_validness.pop();
input = input + c;
} while(!string_validness.empty());
int index = 0;
node * tree = build_tree(input, index);
bool result = false;
if(tree != nullptr)
result = check_for_sum(tree, n);
if(result)
cout << "yes" << endl;
else
cout << "no" << endl;
}
return 0;
}
node * build_tree(string input, int & index)
{
++index;
if(input[index] == ')'){
++index;
return nullptr;
}
node * new_node = new node;
new_node->value = 0;
bool negative = false;
if(input[index] == '-'){
negative = true;
++index;
}
while(input[index] >= '0' && input[index] <= '9'){
new_node->value = new_node->value * 10 + (input[index] - '0');
++index;
}
if(negative)
new_node->value *= -1;
new_node->left = build_tree(input, index);
new_node->right = build_tree(input, index);
++index;//to remove the closing paranthesis
return new_node;
}
bool check_for_sum(node * tree, int sum)
{
bool result = false;
if(tree->left != nullptr){
tree->left->value += tree->value;
result |= check_for_sum(tree->left, sum);
}
if(tree->right != nullptr){
tree->right->value += tree->value;
result |= check_for_sum(tree->right, sum);
}
if(tree->left == nullptr && tree->right == nullptr){
if(tree->value == sum)
return true;
else
return false;
}
return result;
}