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

    题目1009:二叉搜索树

            从现在开始打算重启刷题征程。程序员的人生不需要解释!

    这次撇开poj hoj等难度较大的oj系统,从九度入手(已经很长时间没写过代码了抓狂),主要先“叫醒” 沉睡依旧的大脑。唉~真的很长时间没写博客,没写代码了,只能加油吧!

    题目如下

    时间限制:1 秒

    内存限制:32 兆

    特殊判题:

    提交:4310

    解决:1921

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

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

    样例输入:
    2
    567432
    543267
    576342
    0
    样例输出:
    YES
    NO
    来源:
    2010年浙江大学计算机及软件工程研究生机试真题

    题目难度:水题

    解题思路:

            1.构建二叉排序树。(题目中叫做二叉搜索树,其时是一样的,就是对于二叉树的任何一个结点的所有左孩子结点都小于父结点,但是父节点都小于右孩子结点,好吧,我既然还能记得住,看来,大脑锈的不是很厉害)

            2.对构建的二叉排序树尽心前序遍历,得到前序序列。(前序就是 前根序,后序就是 后根序,中序就是 中根序)

            3.比较前根序列,一样就输出YES ,不一样就输出 NO

    c++代码:

         

    [cpp] view plain copy
     
     print?
    1. #include <iostream>  
    2. #include <string.h>  
    3. using namespace std;  
    4.   
    5. struct Node{  
    6.     Node* left ;  
    7.     Node* right ;  
    8.     char data ;  
    9.     Node():left(NULL),right(NULL),data('*'){}  
    10. };  
    11.   
    12. //插入有序树  
    13. void insertSortTree(Node * root, char data){  
    14.     Node * p = root ;  
    15.     Node * q = new Node ;  
    16.     q->data = data ;  
    17.     while(1){  
    18.         if(p->data>data&&p->left)p=p->left;  
    19.         else if(p->data<data&&p->right)p=p->right;  
    20.         else if(p->data>data){  
    21.             p->left = q ;  
    22.             return;  
    23.         }  
    24.         else if(p->data<data){  
    25.             p->right = q ;  
    26.             return;  
    27.         }  
    28.     }  
    29. }  
    30. //销毁有序树  
    31. void destroySortTree(Node * root){  
    32.     Node * p = root ;  
    33.     if(p->left!=NULL)destroySortTree(p->left);  
    34.     if(p->right!=NULL)destroySortTree(p->right);  
    35.     delete p ;  
    36. }  
    37. //创建有序树  
    38. Node * createSortTree(char * datas , int n){  
    39.     if(n<=0)return NULL;  
    40.     Node * root = new Node ;  
    41.     root->data = datas[0];  
    42.     Node * p = root ;  
    43.     for(int i = 1 ; i < n ;i++){  
    44.         insertSortTree(root,datas[i]);  
    45.     }  
    46.     return root ;  
    47. }  
    48. //用到的变量  
    49. char x1[11];int ct1 = 0;  
    50. char x2[11];int ct2 = 0;  
    51. //前根序遍历  
    52. void pre_lst(Node * root,char* x,int* index){  
    53.     if(root)x[(*index)++] = root->data ;  
    54.     if(root->left)pre_lst(root->left,x,index) ;  
    55.     if(root->right)pre_lst(root->right,x,index) ;  
    56. }  
    57. //比较前根序列  
    58. bool cmp(int n){  
    59.     for(int i = 0 ; i < n ; i++){  
    60.         if(x1[i]!=x2[i])return false;  
    61.     }  
    62.     return true ;  
    63. }  
    64. int main()  
    65. {  
    66.     int T ;  
    67.     int n;  
    68.     Node * root1 = NULL;  
    69.     Node * root2 = NULL;  
    70.     int index1 ,index2 ;  
    71.     while(1){  
    72.         cin>>T ;if(T==0)break;  
    73.         cin>>x1 ;  
    74.         ct1 = strlen(x1);  
    75.         root1 = createSortTree(x1,ct1);  
    76.         index1 = 0 ;  
    77.         pre_lst(root1,x1,&index1) ;  
    78.         for(int i = 0 ; i < T ; i++ ){  
    79.             cin>>x2 ;  
    80.             ct2 = strlen(x2);  
    81.             root2 = createSortTree(x2,ct2);  
    82.             index2 = 0 ;  
    83.             pre_lst(root2,x2,&index2) ;  
    84.             if(cmp(ct1)){  
    85.                 cout<<"YES"<<endl;  
    86.             }else cout<<"NO"<<endl;  
    87.             destroySortTree(root2) ;  
    88.         }  
    89.         destroySortTree(root1) ;  
    90.   
    91.     }  
    92.   
    93.     return 0;  
    94. }  


    好了,写代码的感觉来了。    

     

  • 相关阅读:
    深入浅出 Java Concurrency (7): 锁机制 part 2 AQS[转]
    深入浅出 Java Concurrency (6): 锁机制 part 1[转]
    深入浅出 Java Concurrency (5): 原子操作 part 4[转]
    深入浅出 Java Concurrency (4): 原子操作 part 3 指令重排序与happens-before法则[转]
    深入浅出 Java Concurrency (3): 原子操作 part 2[转]
    深入浅出 Java Concurrency (2): 原子操作 part 1[转]
    深入浅出 Java Concurrency (1) : J.U.C的整体认识[转]
    深入浅出 Java Concurrency
    读书笔记--模式与重构 目录
    SpringMVC,3种不同的URL路由配置方法 [转]
  • 原文地址:https://www.cnblogs.com/yzm10/p/7227961.html
Copyright © 2011-2022 走看看