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

    http://acm.hdu.edu.cn/showproblem.php?pid=3791

    二叉搜索树

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

    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
     
    分析:
    二叉排序树(Binary Sort Tree)又称二叉查找树。 它或者是一棵空树;或者是具有下列性质的二叉树
    (1)若左子树不空,则左子树上所有结点的值均小于它的根结点的值;
    (2)若右子树不空,则右子树上所有结点的值均大于它的根结点的值;
    (3)左、右子树也分别为二叉排序树;
    这里我们只要建树,然后比较两棵树是不是一样就ok
     
    代码:
     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<string.h>
     4 #include<stdlib.h>
     5 using namespace std;
     6 char ctree[100],tree[100];
     7 void buildTree(char *str,char *tree,int p,int i,int len)
     8 {
     9     if(i>=len)
    10         return;
    11     if(str[i]>tree[p])  //若大于根节点  则往右孩子查找
    12     {
    13         if(tree[2*p+1]==0) //没有右孩子
    14         {
    15             tree[2*p+1]=str[i];  
    16             buildTree(str,tree,1,i+1,len);//找到位置,重新回到第一个节点,进行下一个值得查找
    17         }
    18         else  //有右孩子则继续查找
    19         {
    20             buildTree(str,tree,2*p+1,i,len);
    21         }
    22     }
    23     else   //若小于根节点  则往左孩子查找
    24     {
    25         if(tree[2*p]==0)  //没有左孩子
    26         {
    27             tree[2*p]=str[i];
    28             buildTree(str,tree,1,i+1,len);//找到位置,重新回到第一个节点,进行下一个值得查找
    29         }
    30         else  //有左孩子则继续查找
    31         {
    32             buildTree(str,tree,2*p,i,len);  
    33         }
    34     }
    35 }
    36 
    37 int main()
    38 {
    39     int n;
    40     char str[20],tst[20];
    41     while(cin>>n&&n)
    42     {
    43         cin>>str;
    44         int len1=strlen(str);
    45         memset(tree,0,sizeof(tree));
    46         buildTree(str,tree,0,0,len1);
    47         while(n--)
    48         {
    49             cin>>tst;
    50             memset(ctree,0,sizeof(ctree));
    51             int len2=strlen(tst);
    52             buildTree(tst,ctree,0,0,len2);
    53             int i;
    54             for(i=0;i<50;i++)
    55                 if(ctree[i]!=tree[i])
    56                     break;
    57             if(i>=50)
    58                 puts("YES");
    59             else
    60                 puts("NO");
    61         }
    62     }
    63     return 0;
    64 }
     
  • 相关阅读:
    ingress-nginx-controller 504 gateway time-out 问题
    ansible的shell模板使用awk包含引号的问题
    Python selenium模块报错解决
    redis密码破解(multiprocessing的Pool多进程模式)-join方法小坑
    redis密码破解(Python使用multiprocessing分布式进程)
    redis密码破解(python使用redis模块)
    redis密码破解(python使用socket模块)
    修改云主机快照方式为live snapshot
    虚拟机重启错误,libvirtError:internal error:process exited while connecting to monitor
    如何解决高并发秒杀的超卖问题
  • 原文地址:https://www.cnblogs.com/crazyapple/p/3077382.html
Copyright © 2011-2022 走看看