The following code results in a compile error, anyone got a clue why this happens?
#include <iostream>
using namespace std;
class TreeNode {
public:
int value;
TreeNode *left, *right;
TreeNode(int value) {
this->value = value;
}
bool contains(int total, int help) {
if(left == NULL && right == NULL) {
return total == help+value;
}
return ((left != NULL && left->contains(total, help+value))
|| (right != NULL && right->contains(total, help+value)));
}
};
int nextToken() {
while(!cin.eof()) {
char next = cin.peek();
if(next == '(' || next == ')') {
return cin.get();
} else if ((next >= '0' && next <= '9') || next == '-') {
int ret;
cin >> ret;
return ret;
}
cin.get();
}
exit(0);
}
TreeNode* read() {
int next = nextToken();
if(next != '(') {
cout << "Expected \'(\', but got \'" << next << "\'\n";
exit(1);
}
next = nextToken();
if(next == ')') { // empty tree ()
return NULL;
} else { // non-empty tree (int tree tree)
TreeNode *node = new TreeNode(next);
node->value = next;
node->left = read();
node->right = read();
next = nextToken();
if(next != ')') {
cout << "Expected \')\', but got \'" << (char)next << "\'\n";
exit(1);
}
return node;
}
}
int main() {
int seek;
while(!cin.eof()) {
cin >> seek;
cout << "Seeking " <<seek<<" ... ";
TreeNode* node = read();
string result = (node!=NULL && node->contains(seek, 0))?"yes":"no";
cout << result;
cout<<"\n";
}
return 0;
}
compile error
Moderator: Board moderators