Tree Recovery
来源:互联网 发布:弹奏音乐的软件 编辑:程序博客网 时间:2024/05/23 18:04
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
本想找规律直接输出后序序列,无奈功力尚浅,还是老老实实建树再遍历。
#include <iostream>#include<cstring>#include<cstdlib>using namespace std;struct Node{ char ch; Node *rnode,*lnode;};typedef Node* Tree;char pre[30],in[30];Tree BuildTree(char *pre,char *in,int n);void PostTraverse(Tree t);int main(){ while(cin>>pre>>in){ int len=strlen(pre); Tree t; t=BuildTree(pre,in,len); PostTraverse(t); cout<<endl; } return 0;}Tree BuildTree(char *pre,char *in,int n){ Tree root; char *p,*q; int i; if(n<=0) return NULL; root=(Tree)malloc(sizeof(Node)); root->ch=pre[0]; root->lnode=root->rnode=NULL; i=0; while(i<n){ if(pre[0]==in[i]) break; ++i; } p=pre+1; q=in; root->lnode=BuildTree(p,q,i); p=pre+i+1; q=in+i+1; root->rnode=BuildTree(p,q,n-i-1); return root;}void PostTraverse(Tree t){ if(t){ PostTraverse(t->lnode); PostTraverse(t->rnode); cout<<t->ch; }}
0 0
- Tree Recovery
- Tree recovery
- Tree Recovery
- Tree Recovery
- Tree Recovery
- poj 2255 Tree Recovery
- Tree Recovery---POJ--2255
- POJ 2255 Tree Recovery
- POJ 2255 Tree Recovery
- poj 2255 Tree Recovery
- POJ 2255: Tree Recovery
- zoj 1944 Tree Recovery
- Tree Recovery解题报告
- hoj Tree Recovery
- POJ 2255 Tree Recovery
- POJ 2255 Tree Recovery
- POJ 2255 Tree Recovery
- POJ2255 Tree Recovery
- Servlet的相对路径与绝对路径的详解
- ubuntu安装sublime
- 为什么说GUI是单线程化的
- nodejs 设计思想杂记四 异步控制流模式
- 正则表达式 - 语法
- Tree Recovery
- 命令行中执行带参数的java程序(Command-Line Arguments)
- ios developer tiny share-20160705
- Cocos2dx 3.x版本Cocos2dxDownloader.java文件报错
- 面试题(单例模式两种写法)
- freeradius client,freeradius server ,mysql安装以及集成总结
- 我用的一些mysql函数--
- AngularJS中$http.post问题
- dup()