zoukankan      html  css  js  c++  java
  • hdu 3791 二叉搜索树

    二叉搜索树

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2549    Accepted Submission(s): 1109


    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
     
    Source
    Recommend
    notonlysuccess
     
     
      1 /**
      2 2
      3 567432
      4 543267
      5 576342
      6 
      7 **/
      8 
      9 #include<iostream>
     10 #include<stdio.h>
     11 #include<cstring>
     12 #include<cstdlib>
     13 using namespace std;
     14 
     15 struct node
     16 {
     17     int rp;
     18     struct node *lchild;
     19     struct node *rchild;
     20 };
     21 struct node *root;
     22 int len=0;
     23 
     24 void mem(struct node *p)
     25 {
     26     p->rp=0;
     27     p->lchild=NULL;
     28     p->rchild=NULL;
     29 }
     30 void insert_ecs(struct node **p,int x)
     31 {
     32     if( (*p) == NULL)
     33     {
     34         (*p)=(struct node*)malloc(sizeof(struct node));
     35         mem(*p);
     36         (*p)->rp=x;
     37         return;
     38     }
     39     if( (*p)->rp>x)
     40         insert_ecs(&(*p)->lchild,x);
     41     else
     42         insert_ecs(&(*p)->rchild,x);
     43 }
     44 
     45 void serch1(struct node *p,int *a)
     46 {
     47     a[++len]=p->rp;
     48     if(p->lchild!=NULL)
     49     serch1(p->lchild,a);
     50     if(p->rchild!=NULL)
     51     serch1(p->rchild,a);
     52 }
     53 void serch2(struct node *p,int *a)
     54 {
     55     if(p->lchild!=NULL)
     56     serch1(p->lchild,a);
     57     a[++len]=p->rp;
     58     if(p->rchild!=NULL)
     59     serch1(p->rchild,a);
     60 }
     61 void delete_ecs(struct node *p)
     62 {
     63      if(p->lchild!=NULL)
     64     delete_ecs(p->lchild);
     65     if(p->rchild!=NULL)
     66    delete_ecs(p->rchild);
     67     free(p);
     68 }
     69 void cs(int *a,int *b,int n)
     70 {
     71     int i;
     72     for(i=1;i<=n;i++)
     73         printf("%d ",a[i]);
     74     printf("
    ");
     75     for(i=1;i<=n;i++)
     76         printf("%d ",b[i]);
     77     printf("
    ");
     78 }
     79 int  main()
     80 {
     81     int n,i,j,k1,k2;
     82     char c[20];
     83     int s1[20],s2[20],s3[20];
     84     while(scanf("%d",&n)>0)
     85     {
     86         if(n==0)break;
     87         scanf("%s",c);
     88         k1=strlen(c);
     89         root=NULL;
     90         for(i=0;i<k1;i++)
     91         {
     92             insert_ecs(&root,c[i]-'0');
     93         }
     94         len=0;
     95         serch1(root,s1);
     96         len=0;
     97         serch2(root,s2);
     98        while(n--)
     99         {
    100             scanf("%s",c);
    101             k2=strlen(c);
    102             if(k2!=k1){ printf("NO
    "); continue;}
    103 
    104             struct node *head=NULL;
    105             for(j=0;j<k2;j++)
    106                 insert_ecs(&head,c[j]-'0');
    107             len=0;
    108             serch1(head,s3);
    109             for(j=1;j<=k1;j++)
    110                 if(s1[j]!=s3[j])break;
    111             if(j<=k1)
    112             {
    113                 printf("NO
    ");
    114                 delete_ecs(head);
    115                 continue;
    116             }
    117             len=0;
    118             serch2(head,s3);
    119 
    120             for(j=1;j<=k2;j++)
    121             if(s2[ j ] !=s3[ j ])break;
    122             if(j<=k2)
    123             {
    124                 delete_ecs(head);
    125                 printf("NO
    ");
    126                 continue;
    127             }
    128             printf("YES
    ");
    129         }
    130         delete_ecs(root);
    131     }
    132     return 0;
    133 }
  • 相关阅读:
    CSS选择器
    CSS的语法规范
    CSS简介
    spring 工厂模式解耦的升级版(多例转单例)
    Html label标签
    Java 多线程入门详解
    Html 表单
    Html 列表
    web项目的建立(idea版本)
    工厂模式详解
  • 原文地址:https://www.cnblogs.com/tom987690183/p/3573935.html
Copyright © 2011-2022 走看看