testcase:
++1 *2 3 *+*4 5 6 7
result:
189
/*
define:
typedef struct TreeNode{
TreeNode* left;
TreeNode* right;
char value;
}TreeNode;
*/
int Count(TreeNode* root)//中序遍历计算
{
int result=0;
if(root)
{
switch (root->value) {
case '+':
result=Count(root->left)+Count(root->right);
break;
case '-':
result=Count(root->left)-Count(root->right);
break;
case '*':
result=Count(root->left)*Count(root->right);
break;
case '/':
result=Count(root->left)/Count(root->right);
break;
default:
result+=root->value-48;
break;
}
}
return result;
}
void PrintExpreession(TreeNode* root){
if(root->left!=NULL){
if(root->left->left!=NULL){
printf("(");
//TODO
}
PrintExpreession(root->left);
if(root->left->left!=NULL){
printf(")");
//TODO
}
}
printf("%c ",root->value);
if(root->right!=NULL){
PrintExpreession(root->right);
//TODO
}
}