#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define OVERFLOW -1
typedef int status;
typedef char TElemType;
//二叉树的二叉链表存储表示
typedef struct BiTNode{
TElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
//按先序序列建立二叉树
status createbitree(BiTree &T){
TElemType ch;
ch=getchar();
if(ch=='#')T=NULL;
else {
if(!(T=(BiTNode *)malloc(sizeof(BiTNode)))) exit(OVERFLOW);
T->data=ch;
createbitree(T->lchild);
createbitree(T->rchild);
}
return OK;
}
//先序遍历二叉树
status preordertraverse(BiTree &T){
if(T){
printf("%c",T->data);
preordertraverse(T->lchild);
preordertraverse(T->rchild);
}
return OK;
}
//求二叉树中结点个数
status countnode(BiTree &T){
if(!T) return 0;
else return (1+countnode(T->lchild)+countnode(T->rchild));
}
//求独生子女数目
status countonlychild(BiTree &T){
if(!T || ((!T->lchild) && (!T->rchild)) || ((T->lchild) && (T->rchild))) return 0;
else return (1+countonlychild(T->lchild)+countonlychild(T->rchild));
}
//求双胞胎对数
status countchildren (BiTree &T){
if(!T||(!T->lchild||!T->rchild)) return 0;
else return (1+countchildren (T->lchild)+countchildren (T->rchild));
}
void main(){
BiTree T;
createbitree(T);
preordertraverse(T);
printf("\n%d\n",countnode(T));
printf("%d\n",countonlychild(T));
printf("%d\n",countchildren (T));
}
先谢了!