POj2255 利用先序,中序输出后序序列

来源:互联网 发布:知秋的小说哪本好看 编辑:程序博客网 时间:2024/05/21 01:50

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
解题思路:题意即有先序和中序得到后遍历,做法有两种。第一,先利用先序遍历得到根结点,再用中序遍历根据根结点得到左右子树, 创建一个二叉树,为它分配空间,初始化所以结点。再构建二叉树,递归所有的子树,得到所有的根存入二叉树。再用后序遍历便可以。第二。利用中序遍历跟后序遍历的关系,中序遍历为左--根--右,后序遍历为左--右--根。只要找到每个子树的根,然后与它的右子树交换就可以了,遍历完所有的子树就可以得到后序遍历。(这样就不用创建二叉树了,节省空间跟时间)  先序是根--左--右。先序,中序,后序三者的关系非常密切
---------------------
#include<iostream>//#include<string.h>//#include<stdlib.h>#include<stdio.h>using namespace std;#define max  100typedef struct node{char data;node  *left;node  *right;}node;char s1[max], s2[max];node* newnode(){node* u = (node*)malloc(sizeof(node));if (u != NULL){u->data = 0;u->left = u->right = NULL;}return u;}node* build(int n, char *s1, char *s2){if (n <= 0){return  NULL;}node *r = newnode();r->data = s1[0];int p = strchr(s2, s1[0]) - s2;  //该节点在S2中的位置r->left = build(p, s1 + 1, s2);  //p代表有p个结点,即根前面的个数都是属于左子树的 //S1可表示该数组的第一个元素,r->right = build(n - p - 1, s1 + 1 + p, s2 + p + 1);//s2 + p + 1用来缩小要寻找的范围//因为数组是从0开始,假设我的根在s1中是第5个数,//却被存在了s2中的第4个位置,而右边应该从第六个数开始,//对应的s2中的位置所以需要加上1return r;}void  postorder(node *root){if (root == NULL){return;}postorder(root->left);postorder(root->right);cout << root->data;}int main(){while (cin >> s1&&cin >> s2)//注意此处可以用scanf("%s%s",s1,s2)==2 此处使用scanf("%s%s",s1,s2)&&!EOF会有错误  至于为什么要等于2我不知道   知道的可以留言或评论  所以两者都不要用  直接输入就好了{int n = strlen(s1);node* root = build(n, s1, s2);//S2时用来确定根结点的位置的postorder(root);printf("\n");}system("pause");return  0;}
0 0
原创粉丝点击