NYOJ--Tree

来源:互联网 发布:做淘宝直播怎么赚钱 编辑:程序博客网 时间:2024/06/05 15:20

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 ABCDEFGBCAD CBAD
样例输出
ACBFGEDCDAB

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

#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;}

0 0
原创粉丝点击