Based on the idea of subsequent traversal, recursively calculate the results of the left and right subtrees, and finally calculate the results according to the operator of the root node
typedef struct BiTNode{
ElemType data;
struct BiTNode *lchild,*rchild;
}*BiTree;
//对二叉链表树中的结点计算
ElemType Calculate(BiTree T){
BiTNode *p = T; //创建指针指向根结点
ElemType val_l, val_r;
if(T){
val_l = Calculate(p->lchild); //递归计算左、右子树
val_r = Calculate(p->rchild);
switch(p->optr){ //根据根节点的字符将左右子结果计算为当前这一层子树的结果
case'+':
value = val_l + val_r;
break;
case'-':
value = val_l - val_r;
break;
case'*':
value = val_l * val_r;
break;
case'/':
value = val_l / val_r;
break;
default:
break;
}
}
return value;
}