zoukankan      html  css  js  c++  java
  • 比较两个二叉排序树是否一样

    题目描述:

        开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束。

        接下去一行是一个序列,序列长度小于10,包含(0~9)的数字,没有重复数字,根据这个序列可以构造出一颗二叉搜索树。

        接下去的n行有n个序列,每个序列格式跟第一个序列一样,请判断这两个序列是否能组成同一颗二叉搜索树。

    如果序列相同则输出YES,否则输出NO

        

        由于数据量比较的小,所以可以采用空间换取时间的方法,我们这里并没有构建二叉树,而是使用数组存储输入的value。

    如果父节点的数组索引是index。那么它的左孩子的数组索引就是2*index + 1。右孩子的数组索引为2*index。当然也可以反过来。

    代码实现如下:

     1 #include <iostream>
     2 #define MAX 1024
     3 using namespace std;
     4 
     5 //strinput用来接收输入的字符串。sourceTree是其他的和它比较的树。generTree是待比较的输入树。这里树使用数组存储。
     6 char sourceTree[MAX] , generTree[MAX], strinput[10];
     7 
     8 bool insertNode(char tree[], char c)
     9 {
    10     int index = 1;//从下标为1开始构建
    11     bool mark = true;
    12 
    13     while (1)
    14     {
    15         if (tree[index] == '')//还没有填入字符
    16         {
    17             tree[index] = c;
    18             if (tree[index] != sourceTree[index])//比较新构建的树和原来的是否一样
    19             {
    20                 mark = false;                
    21             }
    22             break;
    23         }
    24 
    25         if (tree[index] < c)
    26         {
    27             index = index * 2 + 1;
    28         }
    29         else
    30         {
    31             index = index * 2;
    32         }
    33     }
    34     return mark;
    35 
    36 }
    37 
    38 bool buildTree(char tree[], char strinput[], int len)
    39 {
    40     int i;
    41     bool mark = true;
    42 
    43     for (i = 0; i < len; i++)
    44     {
    45         //每调用一次为数组赋一个值
    46         mark = insertNode(tree, strinput[i]);
    47         if (!mark)
    48         {
    49             break;
    50         }
    51     }
    52     return mark;
    53 }
    54 int test()
    55 {
    56     int n;
    57     bool mark;
    58     while (cin >> n && n)
    59     {
    60         cin >> strinput;
    61         int len = strlen(strinput);
    62         //初始化
    63         memset(sourceTree, 0, sizeof(sourceTree));
    64         //建立原始的树,即其他的要和它进行比较
    65         buildTree(sourceTree, strinput, len);
    66         while (n--)
    67         {
    68             cin >> strinput;
    69             len = strlen(strinput);
    70             mark = true;
    71             //初始化
    72             memset(generTree, 0, sizeof(generTree));
    73             //输入的待比较的树
    74             mark = buildTree(generTree, strinput, len);
    75             if (mark)
    76             {
    77                 cout << "YES" << endl;
    78             }
    79             else
    80             {
    81                 cout << "NO" << endl;
    82             }
    83         }
    84     }
    85     return 0;
    86 }
  • 相关阅读:
    CSS3 target伪类简介
    不用position,让div垂直居中
    css3 在线编辑工具 连兼容都写好了
    a标签伪类的顺序
    oncopy和onpaste
    【leetcode】1523. Count Odd Numbers in an Interval Range
    【leetcode】1518. Water Bottles
    【leetcode】1514. Path with Maximum Probability
    【leetcode】1513. Number of Substrings With Only 1s
    【leetcode】1512. Number of Good Pairs
  • 原文地址:https://www.cnblogs.com/leewhite/p/5342403.html
Copyright © 2011-2022 走看看