A - Tree Recovery(先序中序求后序的模板)
来源:互联网 发布:有序数组和无序数组 编辑:程序博客网 时间:2024/05/12 04:22
A - Tree Recovery
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64uDescription
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 <algorithm>#include <string.h>#include <stdio.h>#include <iostream>#define N 30using namespace std;struct tree{ char data; struct tree *right, *left;};struct tree *create ( int len, char *Prch, char *Inch ){ struct tree *root; int i; root = (struct tree *)malloc(sizeof (struct tree)); /*That is no charactors to convert 已经没有字符可以转化了*/ if ( len == 0 ) { return root = NULL; } root->data = Prch[0]; /*Find the position of the first charactors string in the Inch 找到第一个字符所在Inch的位置*/ for ( i = 0; i < len ; i++ ) { if ( Prch[0] == Inch[i] ) { break; } } /*The most important part 最重要的部分*/ root->left = create ( i, Prch+1, Inch ); root->right = create ( len-i-1, Prch+i+1, Inch+i+1 ); return root;};void AFTER_order ( struct tree *root ){ if ( root ) { AFTER_order ( root->left ); AFTER_order ( root->right ); printf ( "%c", root->data ); }}int main(){ char Inch[N], Prch[N]; while ( ~scanf ( "%s %s", Prch, Inch ) ) { struct tree *root; int len = strlen( Inch ); /*established two forks tree 建立二叉树*/ root = create ( len, Prch, Inch ); /*After order output 后序输出*/ AFTER_order ( root ); printf ( "\n" ); }}
代码菜鸟,如有错误,请多包涵!!!
0 0
- A - Tree Recovery(先序中序求后序的模板)
- A - Tree Recovery(9.3.1)
- A - Tree Recovery(已知前序中序 求后序)
- Tree Recovery
- Tree recovery
- Tree Recovery
- Tree Recovery
- Tree Recovery
- poj2255~~Tree Recovery (树的遍历构造~)
- POJ2255 Tree Recovery 【树的遍历】
- POJ 2255 Tree Recovery(先序中序求后序)
- 树链剖分模板 Query on a Tree
- poj 2255 Tree Recovery
- Tree Recovery---POJ--2255
- POJ 2255 Tree Recovery
- POJ 2255 Tree Recovery
- poj 2255 Tree Recovery
- POJ 2255: Tree Recovery
- POJ 1753 Flip Game(枚举)
- day09_python Tcp/ip协议
- 杭电OJ 5818 Joint Stacks
- C#中USB转串口的拔插捕获
- 小火山的跳子游戏
- A - Tree Recovery(先序中序求后序的模板)
- 2016多校7 HDU 5812 Distance
- JAVA Collections--Collections
- Centos 6.5 Install VMware Player
- iOS 安装Xcode 7.3后找不到模拟器
- 关于app接口调用时如何使session和token票据来保证接口调用的安全
- python中hex,oct,chr,ord函数讲解
- 威海宽带测试
- Python中lambda表达式学习