POJ 2255 Tree Recovery && Ulm Local 1997 Tree Recovery (二叉树的前中后序遍历)

时间:2022-11-14 07:39:39

链接:poj.org/problem?id=2255

本文链接:http://www.cnblogs.com/Ash-ly/p/5463375.html

题意:

  分别给你一个二叉树的前序遍历序列和中序遍历序列,让你给出这个二叉树的后序遍历序列.

思路:

  对于二叉树的三种遍历方式,都可以使用递归来实现,那么也一定可以使用递归来拆解,以达到从遍历序列确定二叉树具体结构的目的.对于前序遍历来说,第一个字母一定是根,并且在序列中根的左子树包含的点一定出现在根的右子树的前面.对于中序遍历序列来说,根前面出现的字母所代表的点一定出现在左子树中,根后面出现的字母所代表的点一定出现在右子树中.在根据前序与中序遍历序列还原二叉树的过程中,先由前序遍历序列确定根节点,再由根节点从中序遍历序列中确定左子树和右子树的中序遍历序列,再由左子树和右子树的中序遍历序列中的元素数目在前序遍历序列中去除根节点后截取出左子树以及右子树的前序遍历序列.然后,一颗二叉树被拆分成根节点,左子树,右子树,以及他们的前序及中序遍历序列,然后分别对左子树及右子树进行递归就可以得到答案.

代码:

 #include <iostream>
#include <cstring>
#include <cstdio> using namespace std;
typedef long long LL;
const int maxN = ; char preord[maxN], inord[maxN];
void recover(int preleft, int preright, int inleft, int inright)
{
int root, leftsize, rightsize; //find root in inorder traversal
for (root = inleft; root <= inright; root++)
if (preord[preleft] == inord[root]) break; //compute sizes of subtrees
leftsize = root - inleft;
rightsize = inright - root; //recover subtrees
if(leftsize > ) recover(preleft + , preleft + leftsize, inleft, root - );
if(rightsize > ) recover(preleft + leftsize + , preright, root + , inright); //root
printf("%c", inord[root]);
} void solve()
{
int len = strlen(preord);
recover(, len - , , len - );
printf("\n");
} int main()
{
freopen("input.txt", "r", stdin);
while (~scanf("%s%s", preord, inord))solve();
return ;
}

代码二:

 #include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector> using namespace std;
const int MAXN = ; void getLRD(char fir[MAXN + ], char mid[MAXN + ]){
int lenf = strlen(fir), lenm = strlen(mid);
char root = fir[];
//find leftsuntree DLR ans LDR
char leftreeDLR[MAXN + ] = {}, leftreeLDR[MAXN + ] = {};
int i;
for(i = ; mid[i] != root && i < lenm; i++) leftreeLDR[i] = mid[i];
leftreeLDR[i] = '\0';
int j;
for(j = ; j < i; j++) leftreeDLR[j] = fir[j + ];
leftreeDLR[j] = '\0';
if(i < && j < )printf("%s", leftreeDLR);
else getLRD(leftreeDLR, leftreeLDR);
  //find rightsuntree DLR ans LDR
char rightreeDLR[MAXN + ] = {}, rightreeLDR[MAXN + ] = {};
int ii;
for(ii = ; ii < lenm - - i; ii++) rightreeLDR[ii] = mid[ii + i + ];
rightreeLDR[ii] = '\0';
int jj;
for(jj = ; jj < ii; jj++) rightreeDLR[jj] = fir[jj + j + ];
rightreeDLR[jj] = '\0';
if(ii < && jj < )printf("%s", rightreeDLR);
else getLRD(rightreeDLR, rightreeLDR);
  //root
printf("%c", root);
} int main()
{
//freopen("input.txt", "r", stdin);
char DLR[MAXN+], LDR[MAXN + ];
while(~scanf("%s%s", DLR, LDR)){
char LRD[MAXN] = {};
getLRD(DLR, LDR);
printf("\n");
}
return ;
}