UVa536 - Tree Recovery

题意

由前序遍历和中序遍历输出后序遍历

思路

前序遍历的第一个字母为根节点,从中序遍历中找到根节点的位置,其左边为左子树,右边为右子树,递归。

总结

从网上找了由前序遍历和中序遍历输出后序遍历 和 由中序遍历和后序遍历输出前序遍历 的代码,po在下面

不能由前序遍历和后序遍历得到中序遍历

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 typedef long long LL;
 7 const int maxn = 30;
 8 char pre[maxn], in[maxn];
 9 void Build_PostTree(char *in, char *pre, int len)
10 {
11     if(!len) return;
12     int i = 0;
13     for( ; i < len; i++)
14         if(in[i] == *pre) break;
15     Build_PostTree(in, pre+1, i); //Left
16     Build_PostTree(in + i + 1, pre + i + 1, len - i - 1); // Right
17     cout << *pre;
18     return;
19 }
20 int main()
21 {
22     //freopen("in.txt","r",stdin);
23     while(scanf("%s %s", pre, in) != EOF){
24         int len = strlen(pre);
25         Build_PostTree(in, pre, len);
26         cout << endl;
27     }
28     return 0;
29 }

由中序遍历和后序遍历输出前序遍历:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 typedef long long LL;
 7 const int maxn = 30;
 8 char in[maxn], post[maxn];
 9 void Build_PostTree(char *in, char *post, int len)
10 {
11     if(len == 0) return;
12     cout << *(post + len - 1);
13     int i = 0;
14     for( ; i < len; i++)
15         if(in[i] == *(post + len - 1)) break;
16     Build_PostTree(in, post, i); //Left
17     Build_PostTree(in + i + 1, post + i, len - i - 1); // Right
18     return ;
19 }
20 int main()
21 {
22    // freopen("in.txt","r",stdin);
23     while(scanf("%s %s", in, post) != EOF){
24         int len = strlen(post);
25         Build_PostTree(in, post, len);
26         cout << endl;
27     }
28     return 0;
29 }
时间: 2024-11-05 04:07:30

UVa536 - Tree Recovery的相关文章

UVA536 - Tree Recovery(递归)

题目:UVA536 - Tree Recovery(递归) 题目大意:给出一棵二叉树的前序遍历和中序遍历,求后序遍历. 解题思路:根据前序遍历将中序遍历的序列分成一棵棵子树,知道这个子树只有一个节点,然后就可以将它按顺序放到后序数组值中了. 代码: #include <cstdio> #include <cstring> const int N = 30; char preord[N], inord[N]; char postord[N]; int p1, p2, len; int

UVa536 Tree Recovery (二叉树遍历)

链接:http://acm.hust.edu.cn/vjudge/problem/19645分析:三种树的遍历方式中除了中序外再知道另外遍历方式得到的序列就可以唯一确定一棵二叉树.先序:先打印根再遍历左右子树.中序:先遍历左子树,然后打印根,再遍历右子树.后序:先遍历左右子树,最后打印根. 1 #include <cstdio> 2 #include <cstring> 3 4 char pre_order[30], in_order[30]; 5 6 void dfs(int L

UVA 536 Tree Recovery(由前序中序求后序,经典)

Tree Recovery Time Limit: 3000 MS Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes. This is an example of one of her creations: D / / B E /

POJ 2255 Tree Recovery 二叉树恢复

一道和Leetcode的一道题目基本上一样的题目. 给出前序遍历和中序遍历序列,要求根据这些信息恢复一颗二叉树的原貌,然后按后序遍历序列输出. Leetcode上有给出后序和中序,恢复二叉树的. 不过其实算法都是一样的.仿佛又回到了做Leetcode题的那段岁月中了. 还有就是输入是我特别处理过的,就两个函数,大家会了的无视,不会的可以学习下. #include <stdio.h> #include <string> #include <algorithm> using

poj 2255 Tree Recovery

Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12413   Accepted: 7765 Description Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital le

poj2255 Tree Recovery

Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11955   Accepted: 7505 Description Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital le

UVa 536 Tree Recovery(先序,中序求后序)

题意  给你二叉树的先序序列和中序序列  求它的后序序列 先序序列的第一个一定是根  中序序列根左边的都属于根的左子树  右边的都属于右子树  递归建树就行了 #include <bits/stdc++.h> using namespace std; typedef struct TNode { char data; TNode *lc, *rc; } node, *BTree; void build(BTree &t, string pre, string in) { t = new

zoj 1944 Tree Recovery (二叉树)

Tree Recovery Time Limit: 2 Seconds      Memory Limit: 65536 KB Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes. This is an example of one

poj2255 Tree Recovery(求后续遍历,二叉树)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=2255 Description Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letter