二叉搜索树 HDU 3791

Problem Description
判断两序列是否为同一二叉搜索树序列
 

Input
开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束。
接下去一行是一个序列,序列长度小于10,包含(0~9)的数字,没有重复数字,根据这个序列可以构造出一颗二叉搜索树。
接下去的n行有n个序列,每个序列格式跟第一个序列一样,请判断这两个序列是否能组成同一颗二叉搜索树。
 

Output
如果序列相同则输出YES,否则输出NO
 

Sample Input
2
567432
543267
576342
0
 

Sample Output
YES
NO

<strong>//二叉搜索树、、、、、、</strong>
<strong>//给定序列,判断两个序列是否为同一颗二叉搜索树的序列;</strong>
<strong>//主要为操作是建树与遍历</strong><img alt="鄙视" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/despise.gif" style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);" />
<strong>
</strong>
<strong><a target=_blank href="http://acm.hdu.edu.cn/showproblem.php?pid=3791">任意门</a>进入原题:</strong>

#include<iostream>
#include<malloc.h>
#include<string>
using namespace std;
#define N 10+2
typedef struct node
{
    char data;
    struct node *lchild,*rchild;
}bitree;
bitree *root;
void creattree(bitree *tree,char c)
{
    if(!root)
    {
        root=(bitree *)malloc(sizeof(bitree));
        root->data=c;
        root->lchild=NULL;
        root->rchild=NULL;
    }
    else
    {
        if(c>tree->data)
        {
            if(!tree->rchild)
            {
                tree->rchild=(bitree *)malloc(sizeof(bitree));
                tree->rchild->data=c;
                tree->rchild->lchild=NULL;
                tree->rchild->rchild=NULL;
            }
            else
            {
                creattree(tree->rchild,c);
            }
        }
        else
        {
            if(!tree->lchild)
            {
                tree->lchild=(bitree *)malloc(sizeof(bitree));
                tree->lchild->data=c;
                tree->lchild->lchild=NULL;
                tree->lchild->rchild=NULL;
            }
            else
            {
                creattree(tree->lchild,c);
            }
        }
    }
}

int coun;
void preorder(bitree *h,string &s)
{
    if(h)
    {
        s[coun++]=h->data;
        //cout<<h->data<<" ";
        preorder(h->lchild,s);
        preorder(h->rchild,s);
    }
}
void free_tree(bitree *h)
{
    if(h)
    {
        free_tree(h->lchild);
        free_tree(h->rchild);
        delete h;
    }
}
int main()
{
    int n;
    string s1,str;
    while(cin>>n,n)
    {
        root=NULL;
        cin>>str;
        coun=0;
        for(int i=0;i<str.size();i++)
        {
            creattree(root,str[i]);
        }
        preorder(root,str);
//         for(int i=0;i<str.size();i++)
//            cout<<str[i];
//         cout<<endl;

        while(n--)
        { root=NULL;
           coun=0;
           cin>>s1;
           for(int i=0;i<s1.size();i++)
           {
              creattree(root,s1[i]);
           }
           preorder(root,s1);
           if(str==s1)
            cout<<"YES"<<endl;
           else
            cout<<"NO"<<endl;
        }
    }//free_tree(root);
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值