#include
#include
typedef char ElemType;
typedef struct BTNode{
ElemType data;
struct BTNode* left;
struct BTNode* right;
}BTreeNode;
BTreeNode *CreateBTree(BTreeNode * HL);
void InOrder(BTreeNode *HL);
int CountTreeLeaf(BTreeNode *HL);
int main()
{
BTreeNode * HL = NULL;
HL = CreateBTree(HL);
InOrder(HL);
printf("%d\n",CountTreeLeaf(HL));
return 0;
}
BTreeNode *CreateBTree(BTreeNode * HL)
{
ElemType ch;
scanf("%c", &ch);
if (ch == ' ')
HL = NULL;
else
{
HL = (BTreeNode *)malloc(sizeof(BTreeNode));
HL->data = ch;
HL->left = CreateBTree(HL->left);
HL->right = CreateBTree(HL->right);
}
return HL;
}
void InOrder(BTreeNode *HL)
{
if (HL != NULL)
{
InOrder(HL->left);
printf("%c ", HL->data);
InOrder(HL->right);
}
}
int CountTreeLeaf(BTreeNode *HL)
{
static int count = 0;
if (HL != NULL)
{
CountTreeLeaf(HL->left);
if (HL->right == NULL && HL->left == NULL)
++count;
CountTreeLeaf(HL->right);
}
return count;
}
int Leaf(const SCTree &T)
{//求森林中叶子结点的个数
if (!T)//如果森林中没有任何一棵树
{
return 0;//则森林中叶子结点个数为0
}
else if (!T->FChild)//否则,如果森林中第一个树没有子树森林
{
return Leaf(T->NextSibling) + 1;//则森林中叶子结点的个数等于1加上森林中除去第一棵树由其他树所构成的
//森林中叶子结点的个数
}
else//否则如果森林中第一棵树存在子树森林
{
return Leaf(T->FChild) + Leaf(T->NextSibling);//则森林中叶子结点的个数等于第一棵树子树森林中叶子结点的个数和森林中由其他树所
//构成的森林中叶子结点个数的和
}
}
typedef struct SCTreeNode
{
TElemtype data;
struct SCTreeNode *FChild, *NextSibling;
}SCTreeNode;
typedef SCTreeNode* SCTree;