1976 - Tree
Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes.
This is an example of one of her creations:
D
/ \
/ \
B E
/ \ \
/ \ \
A C G
/
/
F</pre>
To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree). For the tree drawn above the preorder traversal is DBACEGF and the inorder traversal is ABCDEFG.
She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it).
Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in the same tree.
However, doing the reconstruction by hand, soon turned out to be tedious.
So now she asks you to write a program that does the job for her!
Input
The input will contain one or more test cases.
Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. Both strings consist of unique capital letters. (Thus they are not longer than 26 characters.)
Input is terminated by end of file.
Output
For each test case, recover Valentine's binary tree and print one line containing the tree's postorder traversal (left subtree, right subtree, root).
Examples
Input
DBACEGF ABCDEFG BCAD CBAD
Output
ACBFGED CDAB
Solution C
#include <stdio.h> #include <string.h> #define MAX 30 char preoder[MAX]; char inoder[MAX]; void build(int preleft,int preright,int inleft,int inright) { int i,lsize,rsize; if(preleft <= preright && inleft <= inright) { for(i = inleft;i <= inright;i++) { if(preoder[preleft] == inoder[i]) break; } lsize = i - inleft; rsize = inright - i; if(lsize > 0) build(preleft + 1,preleft + lsize,inleft,i - 1); if(rsize > 0) build(preleft + 1 + lsize,preright,i + 1,inright); printf("%c",preoder[preleft]); } } int main() { //freopen("input","r",stdin); while( scanf("%s %s",preoder,inoder)==2) { int psize,insize; psize = strlen(preoder); insize = strlen(inoder); build(0,psize - 1,0,insize - 1); printf("\n"); } return 0; }
Solution C++
#include<stdio.h> #include<stdlib.h> #include<cstring> #define size 100 typedef struct node//定义结点 { char data; struct node *lchild,*rchild; } Node,*BitTree; int search(char ino[],char c)// 在中序序列中查找先序中该元素所在位置 { int i=0; while(ino[i]!=c&&ino[i]) i++; if(ino[i]==c) return i; } void CrtBT(BitTree &T,char pre[],char ino[],int ps,int is,int n)/*递归算法构造函数,建立二叉链表*/ { int k; if(n==0) T=NULL; else { k=search(ino,pre[ps]);//找到在中序中的位置 分为两部分继续递归 T=(BitTree)malloc(sizeof(Node)); T->data=pre[ps]; if(k==is) T->lchild=NULL;//如果中序中 字符左为空的话 左子树即为空 //先序前进一个 中序不变 字符程度为总长度 k减去 is 既 左边剩下的个数 else CrtBT(T->lchild,pre,ino,ps+1,is,k-is);// if(k==is+n-1) T->rchild=NULL; //同理 else CrtBT(T->rchild,pre,ino,ps+1+(k-is),k+1,n-(k-is)-1); } } void PostOrder(BitTree T) { if(T) { PostOrder(T->lchild); PostOrder(T->rchild); printf("%c",T->data); } } int main() { char pre[size],ino[size]; while(scanf("%s%s",pre,ino)!=EOF) { BitTree T=NULL; CrtBT(T,pre,ino,0,0,strlen(pre)); PostOrder(T); printf("\n"); } }