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

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

题意:

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

思路:

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

代码:

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4
 5 using namespace std;
 6 typedef long long LL;
 7 const int maxN = 31;
 8
 9 char preord[maxN], inord[maxN];
10 void recover(int preleft, int preright, int inleft, int inright)
11 {
12     int root, leftsize, rightsize;
13
14       //find root in inorder traversal
15     for (root = inleft; root <= inright; root++)
16         if (preord[preleft] == inord[root]) break;
17
18       //compute sizes of subtrees
19     leftsize = root - inleft;
20     rightsize = inright - root;
21
22        //recover subtrees
23     if(leftsize > 0)  recover(preleft + 1, preleft + leftsize, inleft, root - 1);
24     if(rightsize > 0) recover(preleft + leftsize + 1, preright, root + 1, inright);
25
26       //root
27     printf("%c", inord[root]);
28 }
29
30 void solve()
31 {
32     int len = strlen(preord);
33     recover(0, len - 1, 0, len - 1);
34     printf("\n");
35 }
36
37 int main()
38 {
39     freopen("input.txt", "r", stdin);
40     while (~scanf("%s%s", preord, inord))solve();
41     return 0;
42 }

代码二:

 1 #include <iostream>
 2 #include <cmath>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <cstdlib>
 6 #include <algorithm>
 7 #include <queue>
 8 #include <stack>
 9 #include <vector>
10
11 using namespace std;
12 const int MAXN = 1000;
13
14 void getLRD(char fir[MAXN + 7], char mid[MAXN + 7]){
15     int lenf = strlen(fir), lenm = strlen(mid);
16     char root = fir[0];
17     //find leftsuntree DLR ans LDR
18     char leftreeDLR[MAXN + 7] = {0}, leftreeLDR[MAXN + 7] = {0};
19     int i;
20     for(i = 0; mid[i] != root && i < lenm; i++) leftreeLDR[i] = mid[i];
21     leftreeLDR[i] = ‘\0‘;
22     int j;
23     for(j = 0; j < i; j++) leftreeDLR[j] = fir[j + 1];
24     leftreeDLR[j] = ‘\0‘;
25     if(i < 2 && j < 2)printf("%s", leftreeDLR);
26     else getLRD(leftreeDLR, leftreeLDR);
27   //find rightsuntree DLR ans LDR
28     char rightreeDLR[MAXN + 7] = {0}, rightreeLDR[MAXN + 7] = {0};
29     int ii;
30     for(ii = 0; ii < lenm - 1 - i; ii++) rightreeLDR[ii] = mid[ii + i + 1];
31     rightreeLDR[ii] = ‘\0‘;
32     int jj;
33     for(jj = 0; jj < ii; jj++) rightreeDLR[jj] = fir[jj + j + 1];
34     rightreeDLR[jj] = ‘\0‘;
35     if(ii < 2 && jj < 2)printf("%s", rightreeDLR);
36     else getLRD(rightreeDLR, rightreeLDR);
37   //root
38     printf("%c", root);
39 }
40
41 int main()
42 {
43     //freopen("input.txt", "r", stdin);
44     char DLR[MAXN+7], LDR[MAXN + 7];
45     while(~scanf("%s%s", DLR, LDR)){
46         char LRD[MAXN] = {0};
47         getLRD(DLR, LDR);
48         printf("\n");
49     }
50     return 0;
51 }  
时间: 2024-11-06 18:19:56

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

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

[抄题]: [思维问题]: 不会递归.三要素:下定义.拆分问题(eg root-root.left).终止条件 [一句话思路]: [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: [一刷]: [二刷]: [三刷]: [四刷]: [五刷]: [总结]: [复杂度]:Time complexity: O() Space complexity: O() [英文数据结构,为什么不用别的数据结构]: [其他解法]: [Follow Up]: [L

Leetcode: Binary Tree Postorder Traversal(二叉树后序遍历)

题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 递归解法(C++版本): /** * Definition for binary

Construct Binary Tree from Inorder and Postorder Traversal ——通过中序、后序遍历得到二叉树

题意:根据二叉树的中序遍历和后序遍历恢复二叉树. 解题思路:看到树首先想到要用递归来解题.以这道题为例:如果一颗二叉树为{1,2,3,4,5,6,7},则中序遍历为{4,2,5,1,6,3,7},后序遍历为{4,5,2,6,7,3,1},我们可以反推回去.由于后序遍历的最后一个节点就是树的根.也就是root=1,然后我们在中序遍历中搜索1,可以看到中序遍历的第四个数是1,也就是root.根据中序遍历的定义,1左边的数{4,2,5}就是左子树的中序遍历,1右边的数{6,3,7}就是右子树的中序遍历

leetCode 106.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 duplicates do not exist in the tree. 思路:这题和上题类似,前序第一个是根节点,后序遍历最后一个是根节点.其余步骤类似. 代码如下: /** * Definition for a binary tree node. * public class TreeNod

[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 that duplicates do not exist in the tree. 这道题要求从中序和后序遍历的结果来重建原二叉树,我们知道中序的遍历顺序是左-根-右,后序的顺序是左-右-根,对于这种树的重建一般都是采用递归来做,可参见我之前的一篇博客Convert Sorted Array to Bin

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

题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 说明: 1) 两种实现,递归与非递归 , 其中非递归有两种方法 2)复杂度分析:时

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

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 经典题目,求二叉树的后序遍历的非递归方法,跟前序,中序,层序一样都需要用到栈,后续的顺序

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 that duplicates do not exist in the tree. 说明: 1)实现与根据先序和中序遍历构造二叉树相似,题目参考请进 算法思想 中序序列:C.B.E.D.F.A.H.G.J.I 后序序列:C.E.F.D.B.H.J.I.G.A 递归思路: 根据后序遍历的特点,知道后序

Tree UVA - 548 已知中序遍历和后序遍历,求这颗二叉树。

You are to determine the value of the leaf node in a given binary tree that is the terminal node of a path of least value from the root of the binary tree to any leaf. The value of a path is the sum of values of nodes along that path. Input The input