树的叶子节点和非叶子结点为什么求不出来啊?
#include<stdio.h>#include<stdlib.h>
#include<malloc.h>
typedef struct Node
{
int data;
struct Node *lchild;
struct Node *rchild;
}BiTree,*BiTNode;
BiTNode InitDSTable()
{
char ch;
BiTNode t;
ch=getchar();
if(ch=='#')
{
return NULL;
}
else
{
t=(BiTNode)malloc(sizeof(BiTree));
t->data=ch;
t->lchild=InitDSTable();
t->rchild=InitDSTable();
}
return t;
}
//递归中序遍历
void InOrderTravese(BiTNode T)
{
if(T)
{
InOrderTravese(T->lchild);
printf("%c",T->data);
InOrderTravese(T->rchild);
}
}
//求叶子的结点数和非叶子结点数
int n1=0,n2=0;//n1非叶子结点,n2为叶子结点数
void Count(BiTNode T)
{
if(T->lchild==NULL && T->rchild==NULL)
n1++;
else
n2++;
Count(T->lchild);
Count(T->rchild);
}
int main()
{
printf("请创建二叉树(#表示指针为空):\n");
BiTNode T=InitDSTable();
printf("中序遍历的结果为:\n");
InOrderTravese(T);
Count(T);
printf("叶子结点数和非叶子结点的个数为:%d,%d",n1,n2);
return 0;
}