poj2255(二叉树重建)
来源:互联网 发布:潜在因子推荐算法 编辑:程序博客网 时间:2024/05/21 14:41
http://poj.org/problem?id=2255
Tree Recovery
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 11367 Accepted: 7130
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
<span style="font-family: Arial, Helvetica, sans-serif;">#include <iostream></span>
#include<cstring>#include<cstdlib>#include<string>#include<cstdio>#include<queue>#include<stack>#include<vector>#include<map>#include<list>#include<set>#define INF 1<<30using namespace std;void bulid(int n,char *s1,char *s2){ if(n<=0) return ; int p=strchr(s2,s1[0])-s2; bulid(p,s1+1,s2); bulid(n-p-1,s1+p+1,s2+p+1); printf("%c",s1[0]);}int main(){ char s1[110],s2[110]; while(scanf("%s %s",s1,s2)!=EOF) { int n=strlen(s1); bulid(n,s1,s2); printf("\n"); } return 0;}
0 0
- poj2255(二叉树重建)
- poj2255(二叉树重建)
- POJ2255,Tree Recovery,二叉树重建
- poj2255树的重建问题
- poj2255- Tree Recovery(二叉树)
- poj2255——Tree Recovery(应用二叉树)
- poj2255 二叉树给出先序中序求后序序列
- poj2255 Tree Recovery 二叉树遍历
- 通过遍历求解二叉树结构poj2255
- POJ2255 Tree Recovery 二叉树遍历
- POJ2255 Tree Recovery(二叉树遍历)
- 重建二叉树(树)
- 重建二叉树(1)
- 二叉树重建(一)
- 二叉树重建(二)
- 重建二叉树(java)
- 二叉树重建(转载)
- poj2255 Tree Recovery 二叉树,这题整我好惨
- Windows下,Eclipse的Android NDK(r8e) 配置
- hdu 1781 Friend
- LeetCode OJ算法题(四十七):Rotate Image
- 编写C语言的技巧
- 引用
- poj2255(二叉树重建)
- Using the NDK plugin
- 验证码处理
- #ifndef,#define,#endif和#pragma once
- JS日期时间选择器
- LINUX 网络爬虫中使用正则匹配URL
- JAVA编译找不到符号
- 字体单位大小对照换算表(字号、磅、英寸、像素)
- 利用SmtpClient发送邮件