按照先序序列创建二叉树T(结点值为字符型, 输入$表示空树),求二叉树的叶子结点数。
求源代码
回复 2楼 书生牛犊
求叶子节点不会写,总是停止工作#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;
}
//求叶子的结点数
int n1=0;
void Count(BiTNode T)
{
if((T->lchild=='$')&&(T->rchild=='$'))
n1++;
Count(T->lchild);
Count(T->rchild);
}
int main()
{
BiTNode T=InitDSTable();
Count(T);
printf("%d",n1);
return 0;
}