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

    题目描述

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

    输入

    开始一个数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<string.h>
     3 char a[10000],b[10000];
     4 void creat(char *x,char *y)
     5 {
     6     x[1]=y[0];
     7     int t=1;
     8     for(int i=1;y[i];i++)
     9     {
    10         while(x[t]!='#')
    11         {
    12             if(y[i]>x[t])
    13             t=2*t+1;
    14             else
    15             t=2*t;
    16         }
    17         x[t]=y[i];
    18         t=1;
    19     }
    20     x[10000]='\0';
    21 }
    22 int main()
    23 {
    24     int n;
    25     char s[15];
    26     while(scanf("%d",&n)&&n)
    27     {
    28         scanf("%s",s);
    29         memset(a,'#',sizeof(a));
    30         creat(a,s);
    31         while(n--)
    32         {
    33             scanf("%s",s);
    34             memset(b,'#',sizeof(b));
    35             creat(b,s);
    36             if(!strcmp(a,b))
    37             puts("YES");
    38             else
    39             puts("NO");
    40         }
    41     }
    42     return 0;
    43 }
    View Code

    http://blog.sina.com.cn/u/2667391517

  • 相关阅读:
    leetcode: Search in Rotated Sorted Array
    leetcode: Linked List Cycle
    leetcode: Longest Valid Parentheses
    leetcode: Next Permutation
    leetcode: Substring with Concatenation of All Words
    leetcode: Divide Two Integers
    leetcode: Implement strStr()
    Spider Studio 新版本 (20131201)
    已知问题汇总 (2013-11-30)
    C#代码获取或设置Iframe中的HTML
  • 原文地址:https://www.cnblogs.com/sdutmyj/p/3225862.html
Copyright © 2011-2022 走看看