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:
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<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
- POj2255 利用先序,中序输出后序序列
- poj2255 Tree Recovery(先序+中序→后序)
- poj2255有树的先序,中序遍历,找出树的后序遍历
- 由先序+后序遍历确定序列是否唯一并输出一个中序序列
- poj2255 根据任意二叉树的先序遍历和中序遍历求解后序遍历
- 已知二叉树先序、中序(中序、后序)遍历序列,输出后序(先序)遍历序列
- HDU1710利用二叉树的先序和中序输出后序
- 已知二叉树的先序遍历和中序遍历序列,输出后序遍历序列
- 算法进化历程之“根据二叉树的先序和中序序列输出后序序列”
- POJ2255 Tree Recovery (先序中序-》后序)
- POJ2255-Tree Recovery 已知先序遍历和中序遍历求后序遍历
- 给定二叉树的先序遍历和中序遍历,输出它的后序遍历序列
- uva536(先序 + 中序输出后序)
- 二叉树建立 先序 中序 后序输出
- 已知二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列
- 已知二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列
- 二叉树:根据二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列
- 【二叉树】 先,中,后序遍历输出
- Android一键退出应用
- Oracle中将查询出的多条记录的某个字段拼接成一个字符串的方法
- Hibernate:hibernate环境搭建并实现hibernate例子
- Android练习-----单选按钮(RadioButton) && 复选框(CheckBox)
- python 中的循环
- POj2255 利用先序,中序输出后序序列
- Spring-春天的故事1
- 数据库系统概论第五版学习笔记连接查询 第三章(三)
- vs的一些事
- jquery 之 Deferred 使用与实现
- 深入解析AsyncTask(doInBackground不工作)
- Atitit atiuse软件系列
- php源码之路第二章第二节(SAPI概述之FastCGI)
- Python源码学习(1)-VS2008编译python源码