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 search (char val) {

	for (int i = 0; i < len; i++)
		if (inord[i] == val)
			return i;
}

void build(int l, int r) {

	if (r - l < 1)
		return;
	int pos = search (preord[p1++]);
	build(l, pos);
	build(pos + 1, r);
	postord[p2++] = inord[pos];
}

int main () {

	while (scanf ("%s%s", preord, inord) != EOF) {

		p1 = p2 = 0;
		len = strlen (preord);
		build(0, len);
		postord[len] = '\0';
		printf ("%s\n", postord);
	}
	return 0;
}
时间: 2024-08-06 02:15:44

UVA536 - Tree Recovery(递归)的相关文章

UVa536 - Tree Recovery

题意 由前序遍历和中序遍历输出后序遍历 思路 前序遍历的第一个字母为根节点,从中序遍历中找到根节点的位置,其左边为左子树,右边为右子树,递归. 总结 从网上找了由前序遍历和中序遍历输出后序遍历 和 由中序遍历和后序遍历输出前序遍历 的代码,po在下面 不能由前序遍历和后序遍历得到中序遍历 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm>

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 /

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 【树的遍历】

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

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