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

     1 /******************************************************************
     2 题目:    二叉搜索树(hdu 3791)
     3 链接:    http://acm.hdu.edu.cn/showproblem.php?pid=3791
     4 算法:    二叉搜索树
     5 思路:    因为二叉搜索树的中序遍历结果是从小到大排序,所以这些
     6            二叉树的中序遍历结果是相同的,所只要求这些树的先序遍历
     7            结果,因为两个二叉树的中序后序遍历结果相同,那么这两个
     8            树相同。所以对每个树先建树然后在找先序就可以了
     9 *******************************************************************/
    10 #include<iostream>
    11 #include<cstdio>
    12 #include<cstring>
    13 #include<cstdlib>
    14 using namespace std;
    15 
    16 typedef struct Tree
    17 {
    18     Tree *left,*right;
    19     int num;
    20 }Tree;
    21 Tree *tree;
    22 int cut;
    23 
    24 void init(Tree *t)
    25 {
    26     t->left=NULL;
    27     t->right=NULL;
    28 }
    29 
    30 Tree *inser(Tree *t,int x)
    31 {
    32     if (t==NULL)
    33     {
    34         t=(Tree *)malloc(sizeof(Tree));
    35         init(t);
    36         t->num=x;
    37         return t;
    38     }
    39     if (x<t->num)
    40     {
    41         t->left=inser(t->left,x);
    42         return t;
    43     }
    44     else
    45     {
    46         t->right=inser(t->right,x);
    47         return t;
    48     }
    49 }
    50 
    51 void build(char *s)
    52 {
    53     tree->num=(s[0]-'0');
    54     for (int i=1;s[i];i++)
    55     {
    56         tree=inser(tree,s[i]-'0');
    57     }
    58 }
    59 
    60 void Find(Tree *t,int *a)
    61 {
    62     if (t==NULL) return ;
    63     a[cut++]=t->num;
    64     Find(t->left,a);
    65     Find(t->right,a);
    66     delete(t);
    67 }
    68 
    69 int main()
    70 {
    71       int n,i;
    72       int a[20],b[20];
    73       while (~scanf("%d",&n)&&n)
    74       {
    75           char s[20];
    76           scanf("%s",s);
    77           tree=(Tree *)malloc(sizeof(Tree));
    78           init(tree);
    79           build(s);
    80           cut=0;
    81           Find(tree,a);
    82           while (n--)
    83           {
    84               tree=(Tree *)malloc(sizeof(Tree));
    85               init(tree);
    86               scanf("%s",s);
    87               build(s);
    88               cut=0;
    89               Find(tree,b);
    90               for (i=0;i<cut;i++)
    91               {
    92                   if (a[i]!=b[i]) break;
    93               }
    94               if (i==cut) printf("YES
    ");
    95               else printf("NO
    ");
    96           }
    97       }
    98 }
  • 相关阅读:
    基于STM32F103C8T6的超声波测距示例
    ST-LINK 到 SWD接线图
    MDK破解版下载
    CompletableFuture Quasar 等并发编程
    c++中参数传递和函数返回简析
    c++中冒号(:)和双冒号(::)的用法
    c++中,size_typt, size_t, ptrdiff_t 简介
    c++中,保证头文件只被编译一次,避免多重包含的方法
    时间序列分析之一次指数平滑法
    Openv2.1基本数据类型
  • 原文地址:https://www.cnblogs.com/pblr/p/5732591.html
Copyright © 2011-2022 走看看