POJ2255 Tree Recovery 二叉树遍历
2015-08-01 09:34
525 查看
Tree Recovery
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!
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
Sample Output
由前序和中序求后序,应该算是比较好理解的吧,和HDU1710一样,具体的参见那篇博客吧,关于遍历的网上一搜也是一堆。
HDU1710
养成释放空间的好习惯。
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12523 | Accepted: 7836 |
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 ABCDEFG BCAD CBAD
Sample Output
ACBFGED CDAB
由前序和中序求后序,应该算是比较好理解的吧,和HDU1710一样,具体的参见那篇博客吧,关于遍历的网上一搜也是一堆。
HDU1710
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> using namespace std; char pre[30],mid[30],n; typedef struct Node{ char v; struct Node *left,*right; }node; node* root; node* newNode(char x){ node* u=(node*)malloc(sizeof(node)); u->left=u->right=NULL; u->v=x; return u; } node* creat(char *pre,int l1,char *mid,int l2){ int i,j,k; for(i=0;i<l2;i++) if(pre[0]==mid[i]) break; node* u=newNode(mid[i]); if(i>0) u->left=creat(pre+1,i,mid,i); if(i<l2-1) u->right=creat(pre+i+1,l2-i-1,mid+i+1,l2-i-1); return u; } void remove_tree(node* u){ if(u==NULL) return ; remove_tree(u->left); remove_tree(u->right); delete u; } void print(node* u){ if(u==NULL) return ; print(u->left); print(u->right); printf("%c",u->v); } int main() { int i,j,n; while(scanf("%s",pre)!=EOF){ scanf("%s",mid); n=strlen(pre); remove_tree(root); root=creat(pre,n,mid,n); print(root); printf("\n"); } return 0; }
养成释放空间的好习惯。
相关文章推荐
- Shiro学习(8)拦截器机制
- 百度地图之添加控件——比例尺、缩略图、平移缩放
- Zigbee系列 学习笔记二(工程文件分析)
- php curl 传递数据
- 关于windows10升级
- 【SpringMVC整合MyBatis】spring参数绑定
- android studio 真机运行出现乱码
- android.os.NetworkOnMainThreadException
- iOS开发UI篇—transframe属性(形变)
- GCJ 2008 Round 1A A 排序贪心
- 三角形示例(兼容IE6)
- Hdu 1281 棋盘游戏 (二分匹配)
- Qt 程序等待多长时间执行
- U - 方格取数(1)
- 可预见的未来-智能摄像头
- Notice: Undefined variable解决办法
- Shiro学习(7)与Web整合
- EJB学习笔记之九(实例详解EJB事务中的六大传播特性)
- Serializable serialVersionUID
- Android控件详解之进度控件