//求节点数
#include
#include
#include
#define TRUE 1
#define FLASE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int TElemType;
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild,*rchild;
} BiTNode,*BiTree;
Status CreateBiTree(BiTree &T)
{
TElemType e;
scanf("%d",&e);
if(e==0) T=NULL;
else
{
T=(BiTree)malloc(sizeof(BiTNode));
if(!T)
exit(OVERFLOW);
T->data =e;
CreateBiTree(T->lchild );
CreateBiTree(T->rchild );
}
return OK;
}
int BTNodeCount (BiTree T)
{
int m=0,n=0;
if(T==NULL)
return 0;
else
{
m=BTNodeCount(T->lchild );
n=BTNodeCount(T->rchild );
return (m+n+1);
}
}
void main()
{
BiTree T;
printf("请输入二叉树中节点的值(int型),0表示空树:\n");
CreateBiTree(T);
printf("该树的节点数是%d\n",BTNodeCount(T));
}
//求叶子数
#include
#include
#include
#define TRUE 1
#define FLASE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int TElemType;
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild,*rchild;
} BiTNode,*BiTree;
Status CreateBiTree(BiTree &T)
{
TElemType e;
scanf("%d",&e);
if(e==0) T=NULL;
else
{
T=(BiTree)malloc(sizeof(BiTNode));
if(!T)
exit(OVERFLOW);
T->data =e;
CreateBiTree(T->lchild );
CreateBiTree(T->rchild );
}
return OK;
}
int LeftBiTree(BiTree T)
{
int m,n;
if(!T)
return 0;
else if(!T->lchild &&!T->rchild )
return 1;
else
{
m=LeftBiTree(T->lchild );
n=LeftBiTree(T->rchild );
return (m+n);
}
}
void main()
{
BiTree T;
printf("请输入树中节点的值(int型),0表示空树:\n");
CreateBiTree(T);
printf("该二叉树的树叶是%d\n",LeftBiTree(T));
}
如果需要两个程序合并,自己修改一下就可以了