Tree Recovery
来源:互联网 发布:乾坤大挪移 知乎 编辑:程序博客网 时间:2024/05/23 16:49
Description
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:
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!
This is an example of one of her creations:
D / \ / \ B E / \ \ / \ \ A C G / / F
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.
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).
Sample Input
DBACEGF ABCDEFGBCAD CBAD
Sample Output
ACBFGEDCDAB
#include<stdio.h>#include<stdlib.h>#include<string.h>typedef struct node{ char data; struct node *l,*r;}tree;tree *creat(char *pre,char *in,int len){ tree *head; if(len<=0) return NULL; head = (tree *)malloc(sizeof(tree)); char *p; head->data = *pre; for(p = in;p!=NULL;p++) if(*p == *pre) break; int lon = p-in; head->l = creat(pre+1,in,lon); head->r = creat(pre+lon+1,p+1,len-lon-1); return head;}/*void cengci(tree *root){ tree *p[51]; p[0] = root; int f = 0,r = 1; while(f < r) { if(p[f]) { printf("%c",p[f]->data); p[r++] = p[f]->l; p[r++] = p[f]->r;// } f++; }}*/void last(tree * root){ if(root) { last(root->l); last(root->r); printf("%c",root->data); }}int main(){ char s0[1000],s1[1000]; while(~scanf("%s%*c",s0)) { scanf("%s%*c",s1); char *in,*pre; pre = s0,in = s1; int len = strlen(s0); tree * root = creat(pre,in,len); last(root); puts(""); //cengci(root); //puts(""); } return 0;}
0 0
- Tree Recovery
- Tree recovery
- Tree Recovery
- Tree Recovery
- Tree Recovery
- poj 2255 Tree Recovery
- Tree Recovery---POJ--2255
- POJ 2255 Tree Recovery
- POJ 2255 Tree Recovery
- poj 2255 Tree Recovery
- POJ 2255: Tree Recovery
- zoj 1944 Tree Recovery
- Tree Recovery解题报告
- hoj Tree Recovery
- POJ 2255 Tree Recovery
- POJ 2255 Tree Recovery
- POJ 2255 Tree Recovery
- POJ2255 Tree Recovery
- 设计模式总结之Chain Of Responsibility Parttert(职责链模式)
- ruby rails server启动
- Eclipse中如何clean项目以及clean的作用
- xshell4的使用经验
- zzuli 1905 Problem A: 小火山的跳子游戏(规律)
- Tree Recovery
- Parcelable
- System.err: java.lang.UnsatisfiedLinkError: Native method not found: com.iflytek.msc.MSC.QISESession
- 最长递增公共子序列dp(hdu 1423 hdu 4512)
- 剑指offer编程
- ImageMagick
- Codevs 1051 接龙游戏
- HTML5总结
- base64的编解码函数