POJ 题目2255Tree Recovery(二叉树)
来源:互联网 发布:华为pon网络管理 编辑:程序博客网 时间:2024/06/10 08:58
Tree Recovery
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 11450 Accepted: 7188
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
两种方法做的
ac代码
#include<stdio.h>#include<string.h>void fun(char pre[],char in[],int n,char post[]){if(n<=0)return;char *p=strchr(in,pre[0]);fun(pre+1,in,p-in,post);//左子树fun(pre+(p-in)+1,p+1,n-(p-in)-1,post+(p-in));//右子树post[n-1]=pre[0];//根}int main(){char pre[30],in[30],post[30];while(scanf("%s%s",pre,in)!=EOF){int n=strlen(pre);getchar();fun(pre,in,n,post);post[n]='\0';printf("%s\n",post);}}
ac代码
#include<stdio.h>#include<string>#include<string.h>#include<iostream>using namespace std;typedef struct tree{int data;struct tree *rchild;struct tree *lchild;}tree1,*tree2;void creat(tree2 &root,string pre,string in){int len=pre.size();if(len==0)root=NULL;else{int p=in.find(pre[0]);if(p==-1)root=NULL;else{root=new tree;root->data=pre[0];if(p==0)root->lchild=NULL;elsecreat(root->lchild,pre.substr(1,p),in.substr(0,p));if(p==len-1)root->rchild=NULL;elsecreat(root->rchild,pre.substr(p+1),in.substr(p+1));}}}void out(tree2 &root){if(root==NULL)return;out(root->lchild);out(root->rchild);printf("%c",root->data);}int main(){string pre,in;while(cin>>pre>>in){tree2 root;creat(root,pre,in);out(root);printf("\n");}}
0 0
- POJ 题目2255Tree 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 2255 Tree Recovery(二叉树的遍历)
- POJ 2255 Tree Recovery(二叉树遍历)
- POJ 2255 Tree Recovery 二叉树的遍历
- POJ 2255 Tree Recovery 二叉树+遍历+递归
- QT移植到ARM
- Linux程序设计:make和makefile
- oeacle几种登录方式
- VBS基础篇 - 内置函数 - Math 函数
- Cubic spline(三次样条插值)
- POJ 题目2255Tree Recovery(二叉树)
- leetcode-single number
- 本来你会觉得没什么但是你真的就这样认为了澳门
- Two Sum
- 【跟我一步一步学Struts2】——拦截器
- 海军司令不小心漏嘴 一句话令美军呆若木鸡
- VC中添加WM_DEVICECHANGE消息(经典)
- poj1330 倍增LCA
- VBS基础篇 - 内置函数 - Format 函数