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 letters in the nodes.

This is an example of one of her creations:

                                               D

                                              /
                                             /
                                            B     E

                                           / \
                                          /   \
                                         A     C     G

                                                    /

                                                   /

                                                  F

To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree). For the tree drawn above the preorder traversal is DBACEGF
and the inorder traversal is ABCDEFG.

She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it).

Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in the same tree.

However, doing the reconstruction by hand, soon turned out to be tedious.

So now she asks you to write a program that does the job for her!

Input

The input will contain one or more test cases.

Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. Both strings consist of unique capital letters. (Thus they are not longer than 26 characters.)

Input is terminated by end of file.

Output

For each test case, recover Valentine‘s binary tree and print one line containing the tree‘s postorder traversal (left subtree, right subtree, root).

Sample Input

DBACEGF ABCDEFG
BCAD CBAD

Sample Output

ACBFGED
CDAB

题意:给出前序和中序遍历,求后续遍历;

思路,找到根,找到左子树,找到右子树

代码一如下:

#include <cstdio>
#include <cstring>
int count;
char a[47],b[47];

void find(int start, int end)
{
	int i;
	if(start > end)
		return;
	char root = a[count++];
	for(i = start; i <= end; i++)
	{
		if(b[i] == root)
		{
			break;
		}
	}
	find(start,i-1);
	find(i+1,end);
	printf("%c",root);
}
int main()
{
	while(scanf("%s%s",a,b)!=EOF)
	{
		count = 0;
		int len = strlen(b);
		find(0,len-1);
		printf("\n");
	}
	return 0;
}

代码二:

#include <stdio.h>
#include <string.h>
#define N 27
char pre[N],in[N];
int id[N];
void print(int a,int b,int c,int d)
{
	int i=id[pre[a]-'A'];
	int j=i-c;
	int k=d-i;
	if(j)   print(a+1,a+j,c,i-1);
	if(k)   print(a+j+1,b,i+1,d);
	printf("%c",pre[a]);
}
int main()
{
	while(~scanf("%s%s",pre,in))
	{
		for(int i=0;in[i];i++)
			id[in[i]-'A']=i;
		int len=strlen(in);
		print(0,len-1,0,len-1);
		puts("");
	}
	return 0;
}

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

时间: 2024-10-26 11:07:23

poj2255 Tree Recovery(求后续遍历,二叉树)的相关文章

已知二叉树的先序,中序遍历,求后续遍历

//已知二叉树的先序,中序遍历,求后续遍历 struct TreeNode { char elem; struct TreeNode* left; struct TreeNode* right; }; TreeNode* BinaryTree(char* inorder,char* preorder,int length) { if(length == 0) return NULL; TreeNode* node = new TreeNode; node->elem = *preorder; i

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

树的前中序遍历_求后续遍历

原文链接:http://blog.csdn.net/feliciafay/article/details/6816871 PreOrder:         GDAFEMHZ InOrder:           ADEFGHMZ PostOrder:       AEFDHZMG 现在,假设仅仅知道前序和中序遍历,如何求后序遍历呢?比如,已知一棵树的前序遍历是”GDAFEMHZ”,而中序遍历是”ADEFGHMZ”应该如何求后续遍历? 第一步,root最简单,前序遍历的第一节点G就是root.

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

POJ2255 Tree Recovery (先序中序-》后序)

B - Tree Recovery Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2255 Appoint description: System Crawler (2016-05-08) Description Little Valentine liked

数据结构 二叉树 已知前序中序遍历求后续遍历的递归实现

代码很短,实现起来也很简单,下面是代码: // // main.cpp // PreMidgetPost // // Created by xin wang on 4/29/15. // Copyright (c) 2015 xin wang. All rights reserved. // #include <iostream> //链表二叉树的节点类 template <class T> class BinaryTreeNode{ public: BinaryTreeNode(

POJ 2255 Tree Recovery 树的遍历,分治 难度:0

http://poj.org/problem?id=2255 #include<cstdio> #include <cstring> using namespace std; const int maxn = 27; char pre[maxn],in[maxn]; char past[maxn]; void tre(int ps,int pe,int is,int ie,int& ind) { int lnum = strchr(in,pre[ps]) - in - is

LeetCode 145 Binary Tree Postorder Traversal(二叉树的后续遍历)+(二叉树、迭代)

翻译 给定一个二叉树,返回其后续遍历的节点的值. 例如: 给定二叉树为 {1, #, 2, 3} 1 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你可以用迭代来完成它吗? 原文 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: Recur

【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal-通过中序和后续遍历还原二叉树

一.描述: 二.思路: 二叉树的中序遍历和前序遍历或和后续遍历能唯一确定一节课二叉树,即2中还原方式都需要中序遍历才能完成: 设二叉树的前序遍历序列为{1, 2, 4, 5, 3, 6},中序遍历序列为{4,2,5,1, 3, 6}:(红色标记表示以还原节点!!!) (1)-前序遍历的第一个节点是二叉树的根节点,{1, 2, 4, 5, 3, 6},对应中序中的位置是{4,2,5,1, 3, 6},所以中序序列中的 '1' 之前的全部元素为左子树元素,'1'之后的为右子树元素: (2)-左子树对