/* 思路
1.采用后序遍历的思想
2.结点分为两类:叶子结点、非叶子结点
3.非叶子结点操作:化 char 为 int
4.叶子结点操作:采用后序遍历的思想,计算当前结点与其左、右孩子结点的运算结果
*/
int op(int a,char Op,int b){
if(Op=='+') return a+b;
if(Op=='-') return a-b;
if(Op=='*') return a*b;
if(Op=='/'){
if(b==0){
cout<<"ERROR"<<endl;
return 0;
}
else
return a/b;
}
}
void comp(BTNode *p){
if(p!=NULL){
if(p->lchild!=NULL && p->rchild!=NULL){
comp(p->lchild);
comp(p->rchild);
return op(p->lchild,p->rchild,p)
}else
return p->data-'0';
}
else return 0;
}
二叉树表达式求值 ~
于 2022-07-21 19:04:14 首次发布