#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct BiTNode
{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
BiTree Create(BiTree T)
{
char ch;
ch=getchar();
if(ch=='#')
T=NULL;
else
{
T=(BiTNode *)malloc(sizeof(BiTNode));
if(!T)printf("Error!");
T->data=ch;
T->lchild=Create(T->lchild);
T->rchild=Create(T->rchild);
}
return T;
}
void Preorder(BiTree T)
{
if(T)
{
printf("%c",T->data);
Preorder(T->lchild);
Preorder(T->rchild);
}
}
void Inorder(BiTree T)
{
if(T)
{
Inorder(T->lchild);
printf("%c",T->data);
Inorder(T->rchild);
}
}
void Postorder(BiTree T)
{
if(T)
{
Postorder(T->lchild);
Postorder(T->rchild);
printf("%c",T->data);
}
}
int leaves(BiTree T)
{
int sum=0,m,n;
if(T)
{
if((!T->lchild)&&(!T->rchild))sum++;
m=leaf(T->lchild);
sum+=m;
n=leaf(T->rchild);
sum+=n;
}
return sum;
}
void main()
{
int sum;
BiTree T;
printf("請以先序遍歷的方式輸入二叉樹(結(jié)點(diǎn)沒有左右孩子時(shí)用以#表示)\n");
T=Create(T);
printf("先序遍歷:");
Preorder(T);
printf("\n");
printf("中序遍歷:");
Inorder(T);
printf("\n");
printf("后序遍歷:");
Postorder(T);
printf("\n");
printf("葉子結(jié)點(diǎn)個(gè)數(shù)為:\n");
sum=leaves(T);
printf("%d",sum);
}
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點(diǎn)擊舉報(bào)。