POJ 2255 Tree Recovery(二叉树)
来源:互联网 发布:淘宝开店钱盾认证 编辑:程序博客网 时间:2024/06/05 10:07
Tree Recovery
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 11397 Accepted: 7155
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
Source
Ulm Local 1997
题意:已知二叉树的先序和中序,求后续序列
#include <stdio.h>#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <sstream>#include <vector>#include <queue>#include <set>#include <map>#include <ctime>using namespace std;typedef long long LL;struct node{char c;struct node *left;struct node *right;};struct node* createTree(char *pr,char *in,int n){node *s;char *p;int k;if(n<=0)return NULL;s = (node *)malloc(sizeof(node));s->c = pr[0];for(p = in;p<in+n;p++){if(*p == *pr)break;}k = p-in;s->left = createTree(pr+1,in,k);s->right = createTree(pr+k+1,p+1,n-k-1);return s;}void order(node *root){if(root != NULL){order(root->left);order(root->right);printf("%c",root->c);}}int main(){char pre[30],ino[30];while(scanf("%s%s",pre,ino)!=EOF){int n = strlen(pre);node *root = createTree(pre,ino,n);order(root);printf("\n");}return 0;}
0 0
- POJ 2255 Tree Recovery ( 二叉树)
- POJ 2255 Tree Recovery(二叉树)
- POJ 2255 Tree Recovery 二叉树遍历
- POJ 2255 Tree Recovery [二叉树]
- Poj 2255-Tree Recovery//二叉树,递归
- POJ 2255 Tree Recovery 二叉树基础
- POJ 2255 Tree Recovery 二叉树恢复
- POJ 2255 Tree Recovery(二叉树重建)
- POJ 2255 Tree Recovery (二叉树遍历)
- poj 2255 Tree Recovery(二叉树)
- 【POJ 2255 Tree Recovery】+ 二叉树
- POJ 2255 Tree Recovery 二叉树
- poj 2255 Tree Recovery(求后序遍历,二叉树)
- poj 2255 Tree Recovery(二叉树的遍历)
- POJ 题目2255Tree Recovery(二叉树)
- POJ 2255 Tree Recovery(二叉树遍历)
- POJ 2255 Tree Recovery 二叉树的遍历
- POJ 2255 Tree Recovery 二叉树+遍历+递归
- Ubuntu频繁报错
- 求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
- JavaScript省市二级联动
- HDU 4406 GPA(网络流-最大费用流)
- 两个页面之间传参数(包括单例模式,sugue,tableView,代理模式)
- POJ 2255 Tree Recovery(二叉树)
- 通过 WPF 强制执行复杂的业务数据规则
- baby step giant step
- poj 2549 --- 传说中的用“桶”防止HASH冲突
- 【BNU Summer Training 2014.08.11】 Nested Segments (线段树,离散化)
- kermit安装 ?SET SPEED has no effect without prior SET LINE -l or -j or -X required错误--》chmod 777 /dev/
- CF 457C Elections 解题报告(线段树)
- Invalidate(TRUE)的闪屏问题
- POJ 3268 Silver Cow Party(SPFA)