扩展二叉树

来源:互联网 发布:淘宝如何部分商品退款 编辑:程序博客网 时间:2024/06/05 18:17

Problem Description

由于先序、中序和后序序列中的任一个都不能唯一确定一棵二叉树,所以对二叉树做如下处理,将二叉树的空结点用·补齐,我们把这样处理后的二叉树称为原二叉树的扩展二叉树,扩展二叉树的先序和后序序列能唯一确定其二叉树。现给出扩展二叉树的先序序列,要求输出其中序和后序序列。

Input

输入有多组数据,对于每组数据就一行为扩展二叉树序列(序列长度不超过50)。

Output

对于每组输入输出两行,分别是该二叉树的中序和后序序列。

Sample Input

ABD..EF..G..C..

Sample Output

DBFEGACDFGEBCA
//解题报告:二叉树的建立与遍历。
//标程:
#include<stdio.h>#include<string.h>#include<malloc.h>typedef struct btnode  {char data;struct btnode *lchild,*rchild;}NODE;char s[100];int i;NODE *creat(NODE *p){NODE *t;if(s[++i]=='.') p=NULL;else {p->data=s[i];t=(NODE *) malloc (sizeof(NODE));p->lchild=creat(t);t=(NODE *) malloc (sizeof(NODE));p->rchild=creat(t);}return p;}void inorder(NODE *root){if(root!=NULL){inorder(root->lchild);printf("%c",root->data);inorder(root->rchild);}return ;}void postorder(NODE *root){   if(root!=NULL)   {   postorder(root->lchild);   postorder(root->rchild);   printf("%c",root->data);   }   return ;}int main(){    //freopen("a.txt","r",stdin);while(scanf("%s",s)!=EOF){i=-1;        NODE *root,*q,n;        q=&n;root=creat(q);inorder(root);printf("\n");postorder(root);printf("\n");}return 0;}