UVa 536 - Tree Recovery 前中序求后序
来源:互联网 发布:苹果mac用户名怎么改 编辑:程序博客网 时间:2024/06/07 07:16
Tree Recovery
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 isABCDEFG.
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 Specification
The input file 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 Specification
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
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int MAX = 26+4;struct Node{char elem;struct Node *left;struct Node *right;};void aftOrder(char *in, char *pre, int length){if(length==0)return ;Node node;node.elem = *pre;int rootIndex = 0;for(;rootIndex<length; rootIndex++){if(in[rootIndex]== *pre)break;}aftOrder(in, pre+1, rootIndex);// 左子树aftOrder(in+rootIndex+1, pre+rootIndex+1,length-(rootIndex+1));// 右子树cout << node.elem;// 输出rootreturn ;}int main(){//freopen("in.txt","r",stdin);char pre[MAX], in[MAX];while(cin>>pre){cin>>in;aftOrder(in, pre, strlen(pre));cout << endl;memset(pre, '\0', sizeof(pre));memset(in, '\0', sizeof(in));}return 0;}
- UVa 536 - Tree Recovery 前中序求后序
- UVA 536 - Tree Recovery
- uva 536Tree Recovery
- uva 536 - Tree Recovery
- UVA 536 Tree Recovery
- UVA 536 Tree Recovery
- UVa, 536 Tree Recovery
- uva 536 - Tree Recovery
- UVa 536 - Tree Recovery
- UVA - 536 Tree Recovery
- UVa 536 Tree Recovery
- UVa 536 - Tree Recovery
- UVA - 536 Tree Recovery
- Uva - 536 - Tree Recovery
- Tree Recovery-UVA 536
- uva 536 - Tree Recovery
- Uva-536 Tree Recovery
- UVA - 536 Tree Recovery
- win7、vs2013编译webkit总结
- 杭州封杀嘀嘀、快的打车的加价功能
- AT指令(中文详解版)二
- Apache Spark - Cluster Mode Overview (Spark集群模式一览)
- ubuntu oracle安装
- UVa 536 - Tree Recovery 前中序求后序
- ubuntu 12.04 samba服务器基本配置
- 【结构体】 项目四。玩日期
- windows GCC环境 UT搭建选型,及 mockcpp的使用小东东
- /proc 查看cpu个数,核数,超线程
- TimeSpan介绍运用
- OpenMP中数据属性相关子句详解(1):private/firstprivate/lastprivate/threadprivate之间的比较
- 虚拟机下linux用yum进行安装及其操作
- UICollectionView详细讲解