链接: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 ;
}

POJ 2255 Tree Recovery && Ulm Local 1997 Tree Recovery (二叉树的前中后序遍历)的更多相关文章

  1. Binary Tree Traversal 二叉树的前中后序遍历

    [抄题]:二叉树前序遍历 [思维问题]: 不会递归.三要素:下定义.拆分问题(eg root-root.left).终止条件 [一句话思路]: 节点非空时往左移,否则新取一个点 再往右移. [输入量] ...

  2. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  3. [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树

    Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...

  4. leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

    题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...

  5. lintcode:Binary Tree Postorder Traversal 二叉树的后序遍历

    题目: 二叉树的后序遍历 给出一棵二叉树,返回其节点值的后序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [3,2,1] 挑战 你能使用非递归实现么? 解题: 递归程序 ...

  6. [Swift]LeetCode106. 从中序与后序遍历序列构造二叉树 | Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  7. [Swift]LeetCode145. 二叉树的后序遍历 | Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...

  8. [Swift]LeetCode889. 根据前序和后序遍历构造二叉树 | Construct Binary Tree from Preorder and Postorder Traversal

    Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...

  9. [LeetCode] N-ary Tree Postorder Traversal N叉树的后序遍历

    Given an n-ary tree, return the postorder traversal of its nodes' values. For example, given a 3-ary ...

随机推荐

  1. 导致SQL执行慢的原因

    索引对大数据的查询速度的提升是非常大的,Explain可以帮你分析SQL语句是否用到相关索引. 索引类似大学图书馆建书目索引,可以提高数据检索的效率,降低数据库的IO成本.MySQL在300万条记录左 ...

  2. BZOJ 1101 [POI2007]Zap | 第一道莫比乌斯反(繁)演(衍)

    题目: http://www.lydsy.com/JudgeOnline/problem.php?id=1101 题解: http://www.cnblogs.com/mrha/p/8203612.h ...

  3. BZOJ5011 [JXOI2017]颜色 【线段树 + 主席树】

    题目链接 BZOJ5011 题解 一定只有我这种智障会用这么奇怪的方法做这道题.. 由题我们知道最后剩余的一定是一个区间,而且区间内的颜色不存在于区间外 所以我们的目的就是为了找到这样的区间的数量 区 ...

  4. 雅礼集训 Day3 T3 w 解题报告

    w 题目背景 \(\frac 14\)遇到了一道水题,双完全不会做,于是去请教小\(\text{D}\).小\(\text{D}\)看了\(0.607^2\)眼就切掉了这题,嘲讽了\(\frac 14 ...

  5. C++——内存使用

    内存分配方式: (1)从静态存储区域分配.内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在.例如全局变量,static变量. (2)在栈上创建.在执行函数时,函数内局部变量的存储单 ...

  6. 获取本地ip地址 C#

    与ipconfig获取的所有信息一致的方法: private void GetIp() { System.Diagnostics.Process cmdp= new System.Diagnostic ...

  7. 【BZOJ 1901】Zju2112 Dynamic Rankings &&【COGS 257】动态排名系统 树状数组套线段树

    外面是树状数组,里面是动态开点线段树,对于查询我们先把有关点找出来,然后一起在线段树上行走,这样就是单个O(log2)的了 #include <cstdio> #include <v ...

  8. 解决mysql的日志文件过大的问题

    https://www.2cto.com/database/201203/122984.html

  9. bzoj 1901 线段树套平衡树+二分答案查询

    我们就建一颗线段树,线段树的每一个节点都是一颗平衡树,对于每个询问来说,我们就二分答案, 查询每个二分到的mid在这个区间里的rank,然后就行了 /************************* ...

  10. how to remove an element in lxml

    import lxml.etree as et xml=""" <groceries> <fruit state="rotten"& ...