输出利用先序遍历创建的二叉树的后序遍历序列(0979)
利用先序递归遍历算法创建二叉树并输出该二叉树的后序遍历序列。先序递归遍历建立二叉树的方法为:按照先序递归遍历的思想将对二叉树结点的抽象访问具体化为根据接收的数据决定是否产生该结点从而实现创建该二叉树的二叉链表存储结构。约定二叉树结点数据为单个大写英文字符。当接收的数据是字符”#”时表示该结点不需要创建,否则创建该结点。最后再输出创建完成的二叉树的后序遍历序列。需要注意输入数据序列中的”#”字符和非”#”字符的序列及个数关系,这会最终决定创建的二叉树的形态。
#include<stdio.h>
#include<stdlib.h>
typedef struct tree
{
char data;
struct tree *lchild;
struct tree *rchild;
} *bitree;
void create(bitree *t)
{
char c;
c=getchar();
if(c=='#')
{
(*t)=NULL;
return;
}
else
(*t)=(bitree)malloc(sizeof(struct tree));
(*t)->data=c;
create(&(*t)->lchild);
create(&(*t)->rchild);
}
void print(bitree p)
{
if(p==NULL)
return ;
else
print(p->lchild);
print(p->rchild);
printf("%c",p->data);
}
int main()
{
bitree p;
create(&p);
print(p);
return 0;
}