链接: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. 纯Div+Css制作的漂亮点击按钮和关闭按钮

    纯Div+Css制作的漂亮点击按钮和关闭按钮,单击点击按钮也有效果.这些都不是图片.

  2. JQuery排错关于$(document).ready(function(){});

    最近写了好多JQuery.也出了很多问题.不知道怎么回事.程序就不往下执行了.很是郁闷. 查了下资料,这里可能会有以下几种原因:1.js文件的引用路径不正确,特别是使用了命名空间,容易造成路径错误,使 ...

  3. DataTable定义

    DataTable是一个临时保存数据的网格虚拟表(表示内存中数据的一个表.).DataTable是ADO dot net 库中的核心对象.它可以被应用在 VB 和 ASP 上.它无须代码就可以简单的绑 ...

  4. [CF1041E]Tree Reconstruction

    题目大意:有一棵树,现在给你每条树边被去掉时,形成的两个联通块中点的最大的编号分别是多少,问满足条件的树存不存在,存在输出方案 题解:一条边的两个编号中较大的一个一定是$n$,否则无解. 开始构造这棵 ...

  5. SPOJ DQUERY (主席树求区间不同数个数)

    题意:找n个数中无修改的区间不同数个数 题解:使用主席树在线做,我们不能使用权值线段树建主席树 我们需要这么想:从左向右添加一到主席树上,添加的是该数字处在的位置 但是如果该数字前面出现过,就在此版本 ...

  6. BZOJ2001 [Hnoi2010]City 城市建设 【CDQ分治 + kruskal】

    题目链接 BZOJ2001 题解 CDQ分治神题... 难想难写.. 比较朴素的思想是对于每个询问都求一遍\(BST\),这样做显然会爆 考虑一下时间都浪费在了什么地方 我们每次求\(BST\)实际上 ...

  7. MySQL使用笔记(二)数据库基本操作

    By francis_hao    Dec 11,2016 数据库是什么 数据库是什么呢?对于MySQL来说,数据库是存储数据库对象的容器,参考[1]中的简单解释是:数据库是一个拥有特定排放顺序的文件 ...

  8. [lucene系列笔记3]用socket把lucene做成一个web服务

    上一篇介绍了用lucene建立索引和搜索,但是那些都只是在本机上运行的,如果希望在服务器上做成web服务该怎么办呢? 一个有效的方法就是用socket通信,这样可以实现后端与前端的独立,也就是不管前端 ...

  9. POJ3259:Wormholes(spfa判负环)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 68097   Accepted: 25374 题目链接: ...

  10. java JDK动态代理的机制

    一:前言 自己在稳固spring的一些特性的时候在网上看到了遮掩的一句话“利用接口的方式,spring aop将默认通过JDK的动态代理来实现代理类,不适用接口时spring aop将使用通过cgli ...