博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2255 Tree Recovery (二叉树)
阅读量:6480 次
发布时间:2019-06-23

本文共 4076 字,大约阅读时间需要 13 分钟。

Tree Recovery
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8946   Accepted: 5647

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:
 
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.
 

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

 

 

1,递归法:

在C语言中 strchr 和 strstr函数都被包含在
头文件中,也就是要调用它们时要在程序前面包含
头文件,也就是写这个语句: #include
,strchr函数原型:char * strchr(char * str, int ch); 功能就是找出在字符串str中第一次出项字符ch的位置,找到就返回该字符位置的指针(也就是返回该字符在字符串中的地址的位置),找不到就返回空指针(就是 null)。strstr 函数原型: char * strstr(char * str1,char * str2);功能就是找出在字符串str1中第一次出项字符串str2的位置(也就是说字符串sr1中要包含有字符串str2), 找到就返回该字符串位置的指针(也就是返回字符串str2在字符串str1中的地址的位置),找不到就返回空指针(就是 null)。

 

#include
#include
char s1[30],s2[30];void build(int len,char *s1,char *s2){ if(len<=0) return ; int loc=strchr(s2,s1[0])-s2; build(loc,s1+1,s2); build(len-loc-1,s1+loc+1,s2+loc+1); printf("%c",s1[0]);}int main(){ //freopen("input.txt","r",stdin); int len; while(scanf("%s%s",s1,s2)!=EOF){ len=strlen(s1); build(len,s1,s2); printf("\n"); } return 0;}

 

二:非递归法

 

#include
#include
#include
#include
using namespace std;string s1,s2;struct node{ int pt; char x; struct node *left; struct node *right;};void postorder(node *root){ if(root==NULL) return ; postorder(root->left); postorder(root->right); cout<
x;}int main(){ //freopen("input.txt","r",stdin); int i,j; while(cin>>s1>>s2){ node *root=new(node); root->left=NULL; root->right=NULL; root->x=s1[0]; root->pt=0; for(j=0;j<(int)s2.length();j++) if(s1[0]==s2[j]) break; root->pt=j; for(i=1;i<(int)s1.length();i++){ for(j=0;j<(int)s2.length();j++) if(s1[i]==s2[j]) break; node *p=root,*pre=NULL; while(p){ pre=p; if(j
pt) p=p->left; else p=p->right; } node *t=new(node); t->left=NULL; t->right=NULL; t->x=s1[i]; t->pt=j; if(j
pt) pre->left=t; else pre->right=t; } postorder(root); cout<

 

 

转载地址:http://ntfuo.baihongyu.com/

你可能感兴趣的文章
Mysql一个表编码的坑,mark一下
查看>>
JS动态事件绑定问题
查看>>
在WPF应用程序中利用IEditableObject接口实现可撤销编辑的对象
查看>>
android 8 wifi wifi 扫描过程
查看>>
phalcon的save方法保存失败?
查看>>
获取任意链接文章正文 API 功能简介
查看>>
js中Math.random()生成指定范围数值的随机数
查看>>
线程类的常见方法介绍
查看>>
Spring连接数据库的几种常用的方式
查看>>
MS CRM 2011 Schedule Report & Email Subscription
查看>>
Linux2.6内核驱动移植参考
查看>>
eclipse打开当前文件所在文件夹
查看>>
去哪儿搜索引擎QSearch设计与实现
查看>>
POJ 2255 Tree Recovery (二叉树)
查看>>
HDU 1026 Ignatius and the Princess I
查看>>
There are two ways for Datatable download as excel
查看>>
TextBox客户端JS赋值 后台获取(转载)
查看>>
PCA误差
查看>>
烦人的数据不一致问题到底怎么解决?——通过“共识”达成数据一致性
查看>>
抽象类详解
查看>>