#include<stdio.h>
#include<stdlib.h>
typedef struct BTNode
{
struct BTNode *lchild,*rchild;
int data;
}BTNode,*btnode;
void createBtree(btnode &T)
{
int t;
scanf("%d",&t);
if(t==0)
T=NULL;
else
{
T=(btnode)malloc(sizeof(BTNode));
T->data=t;
createBtree(T->lchild);
createBtree(T->rchild);
}
}
void link(btnode &p,btnode &head,btnode &tail)
{
if(p)
{
if(p->lchild==NULL&&p->rchild==NULL)
{
if(head==NULL)
{
tail=head=p;
}
else
{
tail->rchild=p;
tail=p;
}
}
link(p->lchild,head,tail);
link(p->rchild,head,tail);
}
}
void print(btnode head,btnode tail)
{
btnode p;
p=head;
while(p!=tail->rchild)
{
printf("%d ",p->data);
p=p->rchild;
}
}
void main()
{
btnode T,head=NULL,tail=NULL;
createBtree(T);
link(T,head,tail);
printf("output the leaves: ");
print(head,tail);
}
输出二叉树的叶子节点
最新推荐文章于 2021-08-22 10:17:08 发布