POJ 2255 Tree Recovery 二叉树

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

Source

Ulm Local 1997


题意:

给出一个二叉树的前序和中序遍历,求后序遍历。

我以前理解的是给出前面的字符,后面的字符的位置代表其大小。比如:

CBAD
大小分别为:1,2,3,4。
好心塞。

题解:

好尴尬。。。。。。。看错题了还能ac,希望不要误导了看过这个代码的小伙伴。这可能也是一种解法尴尬,希望能给大家一些帮助。

理解错误的思路是先建立二叉树,在后序遍历。

理解错误但ac的代码如下:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

typedef struct binarytree//建立二叉树结构体
{
    int key;//父亲节点
    struct binarytree *l;//孩子节点
    struct binarytree *r;
} tree;

int a[40];
char str[40];
char st[40];
int len;
tree *root;

tree *creat(int x)//建立父亲节点
{
    tree *t=(tree *)malloc(sizeof(tree));
    t->l=NULL;
    t->r=NULL;
    t->key=x;//赋值给父亲节点
    return t;
}

tree *insert(tree *s,int x)//将点插入二叉树中
{
    tree *t;
    if(s==NULL)//判断该点是否存在兄弟节点,如果存在不为空
    {
        t=creat(x);
        s=t;
    }
    else
    {
        if(x>s->key)//存在兄弟节点,按照左小右大规则插入节点
            s->r=insert(s->r,x);
        else
            s->l=insert(s->l,x);
    }
    return s;
}

void find(tree *root)//后序遍历二叉树
{
    if(root!=NULL)
    {

        find(root->l);
        find(root->r);
        printf("%c",st[root->key]);

    }
}

int main()
{
    while(~scanf("%s %s",str,st))
    {
        int i,j;
        len=strlen(str);
        for(i=0; i<len; i++)//按顺序给字符排大小
        {
            int k=st[i]-'A'+1;
            a[k]=i;
        }
        root=NULL;
        for(i=0; i<len; i++)
        {
            int k=str[i]-'A'+1;
            root=insert(root,a[k]);
        }
        find(root);
        printf("\n");
    }
}

理解正确ac的代码如下:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef struct binarytree
{
    char key;
    struct binarytree *l,*r;
} tree;

char pr[1100];
char in[1100];
tree *root;

void posttree(char *pro,char *ino ,int len)
{
    if(len==0)
        return;
    binarytree* tree = new binarytree;
    tree->key=*pro;
    int index=0;
    for(; index<len; index++)
    {
        if(ino[index]==*pro)
            break;
    }
    posttree(pro+1,ino,index);
    posttree(pro+index+1,ino+index+1,len-(index+1));
    printf("%c",tree->key);
}

int main()
{
    while(~scanf("%s %s",pr,in))
    {
        int len=strlen(pr);
        posttree(pr,in,len);
        printf("\n");
    }
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值