• SDUT 3373 数据结构实验之查找一:二叉排序树


    数据结构实验之查找一:二叉排序树

    Time Limit: 400MS Memory Limit: 65536KB

    Problem Description

    对应给定的一个序列可以唯一确定一棵二叉排序树。然而,一棵给定的二叉排序树却可以由多种不同的序列得到。例如分别按照序列{3,1,4}和{3,4,1}插入初始为空的二叉排序树,都得到一样的结果。你的任务书对于输入的各种序列,判断它们是否能生成一样的二叉排序树。

    Input

    输入包含若干组测试数据。每组数据的第1行给出两个正整数N (n < = 10)和L,分别是输入序列的元素个数和需要比较的序列个数。第2行给出N个以空格分隔的正整数,作为初始插入序列生成一颗二叉排序树。随后L行,每行给出N个元素,属于L个需要检查的序列。
    简单起见,我们保证每个插入序列都是1到N的一个排列。当读到N为0时,标志输入结束,这组数据不要处理。

    Output

    对每一组需要检查的序列,如果其生成的二叉排序树跟初始序列生成的二叉排序树一样,则输出"Yes",否则输出"No"。

    Example Input

    4 2
    3 1 4 2
    3 4 1 2
    3 2 4 1
    2 1
    2 1
    1 2
    0

    Example Output

    Yes
    No
    No

    DQE:

    二叉排序树,开始理解错题意了233
     
     1 #include <iostream>
     2 #include <cstdio>
     3 using namespace std;
     4 
     5 struct Tree
     6 {
     7     int x;
     8     Tree *lt,*rt;
     9 };
    10 
    11 void insert(Tree *&root,int e)
    12 {
    13     if(!root)
    14     {
    15         Tree *r=new Tree;
    16         r->x=e;
    17         r->lt=r->rt=NULL;
    18         root=r;
    19     }
    20     else
    21     {
    22         if(e<root->x)
    23             insert(root->lt,e);
    24         else if(e>root->x)
    25             insert(root->rt,e);
    26     }
    27 }
    28 
    29 bool cmp(Tree *r,Tree *r2)
    30 {
    31     if(r==NULL&&r2==NULL)
    32         return true;
    33     else if(r==NULL||r2==NULL)
    34         return false;
    35     if(r->x!=r2->x)
    36         return false;
    37     return cmp(r->lt,r2->lt)&&cmp(r->rt,r2->rt);
    38 }
    39 
    40 int main()
    41 {
    42     int n,l;
    43     while(scanf("%d",&n),n!=0)
    44     {
    45         scanf("%d",&l);
    46         Tree *root=NULL;
    47         int i;
    48         for(i=1;i<=n;i++)
    49         {
    50             int e;
    51             scanf("%d",&e);
    52             insert(root,e);
    53         }
    54         int j;
    55         for(j=1;j<=l;j++)
    56         {
    57             Tree *r2=NULL;
    58             for(i=1;i<=n;i++)
    59             {
    60                 int e;
    61                 scanf("%d",&e);
    62                 insert(r2,e);
    63             }
    64             if(cmp(root,r2))
    65                 printf("Yes
    ");
    66             else
    67                 printf("No
    ");
    68         }
    69     }
    70     return 0;
    71 }
    72 
    73 /***************************************************
    74 User name: ***
    75 Result: Accepted
    76 Take time: 0ms
    77 Take Memory: 152KB
    78 Submit time: 2016-11-29 16:54:54
    79 ****************************************************/
  • 相关阅读:
    sqlalchemy
    nginx配置文件,做多个项目代理
    pyspider
    Mysql原理+ 多实例 +表损坏
    Spring 学习-AOP
    java 基础 --- 动态代理和静态代理
    Spring 学习-IoC
    [转]字符编码笔记:ASCII,Unicode 和 UTF-8
    JVM(五) class类文件的结构
    数据结构(四)--- 红黑树(RedBlock-Tree)
  • 原文地址:https://www.cnblogs.com/Leroscox/p/6114316.html
Copyright © 2020-2023  润新知