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

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

Tree Recovery
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7819   Accepted: 4947

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
1 /* 2 1、前序遍历的第一个字母必是 根 3 2、在中序遍历的字母串中找出 根字母,那么根字母左右两边的字符串就分别是它的左、右子树 4 下边是参考别人的代码写的AC代码,有一点不懂就是: 5 为什么那个构造二叉树的函数是有返回值的,但是调用的时候直接用了 没有将返回的指针赋给任何变量,谁给解释下? 6 */ 7 //题意:给出二叉树的先序和中序遍历,让求其后序遍历 8 #include 
9 #include
10 using namespace std;11 12 struct node13 {14 char d;15 node* lc;16 node* rc;17 }root[50];18 19 node* construct(node* r,char a[],char b[],int n) //构造二叉树20 {21 int i;22 if(n == 0) 23 return NULL;24 for (i=0;i
d=a[0];29 r->lc=construct(&r[1], &a[1],&b[0],i);30 r->rc=construct(&r[i+1], &a[i+1],&b[i+1],n-i-1);31 break;32 }33 }34 return r;35 }36 37 void postorder(node* r) //后序遍历38 {39 if (r==NULL)40 return;41 postorder(r->lc);42 postorder(r->rc);43 cout<
d);44 // printf("%c", r->d);45 }46 47 int main()48 {49 char a[50], b[50];50 51 while (cin>>a>>b) 52 {53 construct(&root[0],&a[0],&b[0],strlen(a));54 postorder(&root[0]);55 cout<

 

转载于:https://www.cnblogs.com/dongsheng/archive/2012/07/06/2578929.html

你可能感兴趣的文章
Lintcode: Partition Array
查看>>
[Linux]PHP-FPM与NGINX的两种通讯方式
查看>>
Java实现二分查找
查看>>
架构图-模型
查看>>
黑马程序员_Java基础枚举类型
查看>>
UIImage 和 iOS 图片压缩UIImage / UIImageVIew
查看>>
ajax向后台传递数组
查看>>
疯狂JAVA16课之对象与内存控制
查看>>
[转载]树、森林和二叉树的转换
查看>>
django ORM创建数据库方法
查看>>
php7 新特性整理
查看>>
RabbitMQ、Redis、Memcache、SQLAlchemy
查看>>
知识不是来炫耀的,而是来分享的-----现在的人们却…似乎开始变味了…
查看>>
口胡:[HNOI2011]数学作业
查看>>
数据库锁机制及乐观锁,悲观锁的并发控制
查看>>
03 线程池
查看>>
手机验证码执行流程
查看>>
python 基础 ----- 变量
查看>>
设计模式课程 设计模式精讲 2-2 UML类图讲解
查看>>
Silverlight 的菜单控件。(不是 Toolkit的)
查看>>