POJ 2255 Tree Recovery && Ulm Local 1997 Tree Recovery (二叉树的前中后序遍历)
链接: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 (二叉树的前中后序遍历)的更多相关文章
- Binary Tree Traversal 二叉树的前中后序遍历
[抄题]:二叉树前序遍历 [思维问题]: 不会递归.三要素:下定义.拆分问题(eg root-root.left).终止条件 [一句话思路]: 节点非空时往左移,否则新取一个点 再往右移. [输入量] ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- [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 ...
- leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...
- lintcode:Binary Tree Postorder Traversal 二叉树的后序遍历
题目: 二叉树的后序遍历 给出一棵二叉树,返回其节点值的后序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [3,2,1] 挑战 你能使用非递归实现么? 解题: 递归程序 ...
- [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 ...
- [Swift]LeetCode145. 二叉树的后序遍历 | Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
- [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 ...
- [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 ...
随机推荐
- Android中常见的坑有哪些?
对于安卓开发入门级程序猿而言,由于不熟悉代码.工具等等,掉进一些坑中是难免的,今天小编在网上看到一位大神总结的Android开发中比较常见的坑及其原因和解决办法,赶脚还不错,分享出来,给大家提个醒. ...
- arc073 F many moves(dp + 线段树)
设dp[i][y]表示一个点在x[i],另一个点在y时最小要走的步数 那么有以下转移 对于y != x[i-1]的状态,可以证明,他们直接加|x[i] - x[i-1]|即可(如果有其他方案,不符合对 ...
- 洛谷 P1829 [国家集训队]Crash的数字表格 / JZPTAB 解题报告
[国家集训队]Crash的数字表格 / JZPTAB 题意 求\(\sum\limits_{i=1}^n\sum\limits_{j=1}^mlcm(i,j)\),\(n,m\le 10^7\) 鉴于 ...
- bzoj4145 AMPPZ2014 The Prices 状压dp
这个题.......很可以,很小清晰......反正正经的东西我都没想到:重点在于——————我不会处理那个多出来的路费所以当时我就骚骚的弄了一颗树包状压其实这是一个类01背包的状压在每个状态用01背 ...
- Sed basic and practice
定义:Sed 是针对数据流的非交谈式编辑器,它在命令行下输入编辑命令并指定文件,然后可以在屏幕上看到编辑命令的输出结果. 好处:Sed 在缓冲区内默认逐行处理数据,所以源文件不会被更改和破坏. 格式: ...
- PHP设计模式-工厂模式、单例模式、注册模式
本文参考慕课网<大话PHP设计模式>-第五章内容编写,视频路径为:http://www.imooc.com/video/4876 推荐阅读我之前的文章:php的设计模式 三种基本设计模式, ...
- PhoneGap之自定义插件
PhoneGap:作为原生App,Java(这里面是指Android的)与JavaScript 的通信桥梁,使得我们的混合开发更加得心应手,我是与Android结合的混合开发. 但在这里不得不吐槽一下 ...
- Topcoder SRM 608 div1 题解
Easy(300pts): 题目大意:有n个盒子,一共有S个苹果,每个盒子有多少个苹果不知道,但是知道每个盒子的苹果下限和上限.现在要至少选择X个苹果,问如果要保证无论如何都能获得至少X个苹果,至少需 ...
- bzoj 2152 点剖分
比较裸的点剖分,访问到每个重心时,记录一个b数组, 代表当前子树中长度为i的边的数量是b[i],因为是3的倍数, 所以边长mod 3保存就行了,然后记录一个sum数组,代表 当前子树中一个子节点为根的 ...
- Golang使用amqp发送消息
1.为什么使用信道(channel)而不使用TCP连接发送AMQP命令? 对操作系统来说频繁的建立和销毁TCP连接开销非常昂贵,而操作系统每秒建立的连接是有上限的,性能瓶颈不可避免,而只建立一条TCP ...