Posted on
2007-07-30 12:17
少林
阅读(
578)
评论()
收藏
举报
#include<iostream>

using namespace std;

bool success = false;

class TreeNode


{
int value;

int sumvalue;

TreeNode* lchild;

TreeNode* rchild;

bool isnegative;

bool isroot;
int target;

bool isempty;

public:TreeNode(int _target,int _sumvalue,bool _isroot = false):

target(_target),isroot(_isroot),sumvalue(_sumvalue),


lchild(0),rchild(0),isnegative(false),value(0),isempty(true)
{}
void CreateNode();
};
void TreeNode::CreateNode()


{
char ch;

cin.get(ch);

while(ch != ')')

{
if(ch >= '0' && ch <= '9')

{
value = value * 10 + 0 - '0' + ch;

isempty = false;
}
else if( '-' == ch)

{
isnegative = true;
}
else if(ch == '(')

{
if( isnegative == true)

{
value *= (-1);

isnegative = false;
}
if(lchild == 0)

{
lchild = new TreeNode(target,sumvalue + value);

lchild->CreateNode();
}
else

{
rchild = new TreeNode(target,sumvalue + value);

rchild->CreateNode();
}
}

cin.get(ch);
}

if(0 != rchild && 0 != lchild && true == lchild->isempty && true == rchild->isempty)

{
if(target == sumvalue + value && false == success)

{
cout<<"yes"<<endl;

success = true;
}
}
delete lchild;

delete rchild;
}
int main()


{
int target =0;
char ch;

bool isnegative = false;

cin.get(ch);

while(!cin.eof())

{
if(ch == '(')

{
if(isnegative == true)
target *= (-1);
TreeNode root(target,0,true);

root.CreateNode();

if(success == false)

cout<<"no"<<endl;

success=false;

target=0;

isnegative=false;


}
else if( ch >= '0' && ch <= '9')

{
target = target * 10 + 0 + ch - '0' ;
}
else if( ch == '-')

{
isnegative = true;
}
cin.get(ch);
}
return 0;
}
