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 ...
随机推荐
- HDU 6201 transaction transaction transaction(拆点最长路)
transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 132768/1 ...
- [Leetcode] Remove duplicate from sorted list ii 从已排序的链表中删除重复结点
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [Leetcode] word ladder 单词阶梯
Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...
- BZOJ1934 [Shoi2007]Vote 善意的投票 【最小割】
题目 幼儿园里有n个小朋友打算通过投票来决定睡不睡午觉.对他们来说,这个问题并不是很重要,于是他们决定发扬谦让精神.虽然每个人都有自己的主见,但是为了照顾一下自己朋友的想法,他们也可以投和自己本来意愿 ...
- JavaScript的相等(==)与全等(===)
有段代码如下: view source print? 1 if (![] == []) { 2 //Code 3 } ![] == [],true or false? 我们都知道,ECMA ...
- Oulipo HDU - 1686
The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...
- codeforces 792CDivide by Three(两种方法:模拟、动态规划
传送门:https://codeforces.com/problemset/problem/792/C 题意:给你一个字符串,要求让你删除最少个数的元素,使得最终答案是没有前导0并且是3的倍数. 题解 ...
- 使用UMeditor富文本编辑器上传图片
注:本文系作者原创,但可随意转载. 最近写自己的网站玩儿,写到博客的部分,打算使用UMeditor,因为之前也用过(但是好像没实现图片上传的功能),感觉用起来还比较简单. 不过还是折腾了一下午...遇 ...
- Terminals Project
https://github.com/Terminals-Origin Terminals Project Terminals is a secure, multi tab terminal serv ...
- js密码的匹配正则
匹配的密码是 数字大写或者小写的字母.符号. if(pwd.match(/[\d]/) && pwd.match(/[A-Za-z]/) && pwd.match(/[ ...