zoukankan      html  css  js  c++  java
  • 二叉排序树

    二叉排序树

    Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

    题目描述

    二叉排序树的定义是:或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 它的左、右子树也分别为二叉排序树。 今天我们要判断两序列是否为同一二叉排序树

    输入

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

    输出

     

    示例输入

    2
    123456789
    987654321
    432156789
    0

    示例输出

    NO
    NO

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include<string.h>
     4 typedef struct vode
     5 {
     6     char date;
     7     struct vode *l,*r;
     8 } tree,*tree1;
     9 tree1 creat(tree1 t,char f[])
    10 {
    11     int  i;
    12     int s=strlen(f);
    13     tree1 q=t;
    14     for(i=1;i<=s-1;i++)
    15     {
    16         t=q;
    17         while(1)
    18         {
    19             if(f[i]<t->date)
    20             {
    21                 if(t->l==NULL)
    22                 {
    23                     tree1 p;
    24                     p =(tree1)malloc(sizeof(tree));
    25                     p->l=NULL;
    26                     p->r=NULL;
    27                     p->date=f[i];
    28                     t->l=p;//注意,不是p=t->l,这是易错点
    29                     break;
    30                 }
    31                 else
    32                     t=t->l;
    33             }
    34             else
    35             {
    36                 if(t->r==NULL)
    37                 {
    38                     tree1 p;
    39                     p=(tree1)malloc(sizeof(tree));
    40                     p->date=f[i];
    41                     p->l=NULL;
    42                     p->r=NULL;
    43                     t->r=p;
    44                     break;
    45                 }
    46                 else
    47                     t=t->r;
    48             }
    49         }
    50     }
    51     return q;
    52 }
    53 int s=0;
    54 void preorder(tree1 root,char h[])
    55 {
    56     if(root!=NULL)
    57     {
    58         h[s++]=root->date;
    59         preorder(root->l,h);
    60         preorder(root->r,h);
    61     }
    62 }
    63 void pxs(char f[])
    64 {
    65     tree1 root;
    66     root=(tree1)malloc(sizeof(tree));
    67     root->date=f[0];
    68     root->l=NULL;
    69     root->r=NULL;
    70     root=creat(root,f);
    71     preorder(root,f);
    72 }
    73 int main()
    74 {
    75     int n;
    76     while(scanf("%d",&n)&&n!=0)
    77     {
    78         char f[1000];
    79         scanf("%s",f);
    80         pxs(f);
    81         s=0;
    82         int i;
    83         for(i=0; i<=n-1; i++)
    84         {
    85             char g[100];
    86             scanf("%s",g);
    87             pxs(g);
    88             s=0;
    89             if(strcmp(f,g)==0)printf("YES
    ");
    90             else printf("NO
    ");
    91         }
    92     }
    93     return 0;
    94 }
    View Code
  • 相关阅读:
    Struts2框架中使用Servlet的API示例
    struts2 过滤器和拦截器的区别和使用
    Struts2框架的概述及学习重点
    source .bashrc 报错:virtualenvwrapper.sh: There was a problem running the initialization hooks.
    SE93 创建参数事务
    SM30 客户端 有“不可修改”的状态
    CO借贷标识及转换
    Ubuntu install 错误 E:Unable to locate package
    0CO_PC_01 成本对象控制: 计划/实际数据
    SAP函数PREPARE_STRING:提取字符串中的数字
  • 原文地址:https://www.cnblogs.com/kuangdaoyizhimei/p/3236604.html
Copyright © 2011-2022 走看看