求解的方法
int DsonNodes(BiTree T){
if(T == NULL) return 0;
if(T->lchild && T->rchild){
return DsonNodes(T->lchild) + DsonNodes(T->rchild) + 1;
}
return DsonNodes(T->lchild) + DsonNodes(T->rchild);
}
此方法运行的环境
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define maxsize 100
//二叉树的创建序列
//ab#df###c#e##
//abd##e##cf###
typedef struct BiNode{
char date;
struct BiNode *lchild,*rchild;
}BiNode,*BiTree;
BiNode* CreateBiTree();
void Traverse(BiNode *p);
int DsonNodes(BiTree T);
int main(){
BiTree t = CreateBiTree();
printf("双分支节点的个数:%d",DsonNodes(t));
return 0;
}
//用递归方法以中序遍历的方式创建二叉树
BiNode* CreateBiTree(){
BiNode *p;
char a;
scanf("%c",&a);
if(a=='#')
p=NULL;
else{
p=(BiNode *)malloc(sizeof(BiNode));
p->date=a;
p->lchild=CreateBiTree();
p->rchild=CreateBiTree();
}
return p;
}
//递归方法中序遍历二叉树
void Traverse(BiNode *p){
if(p==NULL){
return;
}
else{
Traverse(p->lchild);
printf("%c",p->date);
Traverse(p->rchild);
}
return;
}