算法思想: 求二叉树双分支结点个数就是当一个结点左右孩子非空
时该结点为双分支结点
//二叉树结构体定义
typedef struct BiTree{
char data;
BiTree *lchild;
BiTree *rchild;
}BiTree;
int DsonNodes(BiTree *T){
if(!T){
return 0;
}
if(T->rchild!=NULL && T->lchild!=NULL) //若结点的左右孩子存在
return 1+DsonNodes(T->rchild)+DsonNodes(T->lchild);
else
return DsonNodes(T->rchild)+DsonNodes(T->lchild);
}
测试数据:
char pre[13]={'#','A','B','D','F','H','J','M','N','C','E','K','G'};
char in[13]={'#','D','B','J','H','M','N','F','A','E','K','C','G'};
char pre1[10]={'#','A','B','C','D','E','F','G','H','I'};
char in1[10]={'#','B','C','A','E','D','G','H','F','I'};
char pre2[6]={'#','A','B','D','E','C'};
char in2[6]={'#','D','B','E','A','C'};
char pre3[5]={'#','A','B','D','C'};
char in3[5]={'#','D','B','A','C'};
cout<<"The number of double-branch nodes is: "<<DsonNodes(PreInCreate(pre,in,1,12,1,12))<<endl;
cout<<"The number of double-branch nodes is: "<<DsonNodes(PreInCreate(pre3,in3,1,4,1,4))<<endl;
cout<<"The number of double-branch nodes is: "<<DsonNodes(PreInCreate(pre1,in1,1,9,1,9))<<endl;