首页 > 其他 > 详细

NYOJ--Tree

时间:2014-04-16 12:38:43      阅读:675      评论:0      收藏:0      [点我收藏+]

Tree

时间限制:1000 ms  |  内存限制:65535 KB
难度:3
描述
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! 
输入
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.
输出
For each test case, recover Valentine‘s binary tree and print one line containing the tree‘s postorder traversal (left subtree, right subtree, root).
样例输入
DBACEGF ABCDEFG
BCAD CBAD
样例输出
ACBFGED
CDAB

解析:不要看是英文题,其实看懂以后发现根刚写的上一篇博客类似哈,先序+中序-->构建二叉树-->输出后序的题目,直接拿着上一篇博客的代码直接水了过去,还是把代码贴一下哈!

#include <stdio.h>
#include <malloc.h>
#include <string.h>
//二叉链表
typedef struct node{
	char data;//节点数据元素
	struct node *lchild;//指向左孩子
	struct node *rchild;//指向右孩子
}BiNode,*BTree;
//利用后序和中序建立二叉树
void GetPreOrder(char *last,char *mid,BTree &T,int len)
{
	if(len==0)
	{
		T = NULL;
		return;
	}
	//取出后序序列中的最后一个节点
	char ch=last[len-1];
	int index=0;
	//在中序序列中进行查找根节点,并用index记录其在序列中的索引
	while(mid[index]!=ch)
	{
		index++;
	}
	//给根节点分配空间
	T=(BTree)malloc(sizeof(BiNode));
	T->data=mid[index];
	//建立左子树
	GetPreOrder(last,mid,T->lchild,index);
	//建立右子树
	GetPreOrder(last+index,mid+index+1,T->rchild,len-index-1);
}
void GetPostOrder(char *prim,char *mid,BTree &T,int len)
{
	if(len==0)
	{
		T=NULL;
		return;
	}
	//提出先序序列中的第一个节点
	char ch=prim[0];
	int index=0;
	//在中序序列中查找当前根节点,并用index记录其在序列中的位置
	while(mid[index]!=ch)
	{
		index++;
	}
	//给根节点分配空间
	T=(BTree)malloc(sizeof(BiNode));
	T->data=mid[index];
	//建立左子树
	GetPostOrder(prim+1,mid,T->lchild,index);
	//建立右子树
	GetPostOrder(prim+index+1,mid+index+1,T->rchild,len-index-1);
}
//先序输出二叉树
void PreOrder(BTree T)
{
	if(T!=NULL)
	{
		printf("%c",T->data);
		PreOrder(T->lchild);
		PreOrder(T->rchild);
	}
}
//后序输出二叉树
void PostOrder(BTree T)
{
	if(T!=NULL)
	{
		PostOrder(T->lchild);
		PostOrder(T->rchild);
		printf("%c",T->data);
	}
}
int main()
{
	char prim[26],mid[26],last[26];
	while(scanf("%s%s",prim,mid)!=EOF)
	{
		BTree T=NULL;
		GetPostOrder(prim,mid,T,strlen(prim));
		PostOrder(T);
// 		GetPostOrder(last,mid,T,strlen(last));
// 		PostOrder(T);
		printf("\n");
	}
	return 0;
}

NYOJ--Tree,布布扣,bubuko.com

NYOJ--Tree

原文:http://blog.csdn.net/computer_liuyun/article/details/23749945

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!