码迷,mamicode.com
首页 > 其他好文 > 详细

Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)

时间:2014-05-14 22:03:25      阅读:487      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   c   

Pat1043代码

题目描述:

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:

  • The left subtree of a node contains only nodes with keys less than the node‘s key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node‘s key.
  • Both the left and right subtrees must also be binary search trees.

If we swap the left and right subtrees of every node, then the resulting tree is called the Mirror Image of a BST.

Now given a sequence of integer keys, you are supposed to tell if it is the preorder traversal sequence of a BST or the mirror image of a BST.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<=1000). Then N integer keys are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first print in a line "YES" if the sequence is the preorder traversal sequence of a BST or the mirror image of a BST, or "NO" if not. Then if the answer is "YES", print in the next line the postorder traversal sequence of that tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

Sample Input 1:
7
8 6 5 7 10 8 11
Sample Output 1:
YES
5 7 6 8 11 10 8
Sample Input 2:
7
8 10 11 8 6 7 5
Sample Output 2:
YES
11 8 10 7 5 6 8
Sample Input 3:
7
8 6 8 5 10 9 11
Sample Output 3:
NO

AC代码:二叉树的重建与遍历;
    1. #include<cstdio>  
    2. #include<cstdlib>  
    3. #define MAX 1005  
    4.   
    5. using namespace std;  
    6.   
    7. typedef struct Node  
    8. {  
    9.     int data;  
    10.     Node *left;  
    11.     Node *right;  
    12. }Node;  
    13.   
    14. int Keys[MAX];  
    15. int PreOrder[MAX];  
    16. int PreOrderImg[MAX];  
    17. int PostOrder[MAX];  
    18. int index=0;  
    19.   
    20. void InsertNode(Node **root,int key,bool Img)  
    21. {  
    22.     if(*root==NULL)  
    23.     {  
    24.         *root=(Node *)malloc(sizeof(Node));  
    25.         if(!(*root))  
    26.         {  
    27.             printf("No enough memory!\n");  
    28.             exit(-1);  
    29.         }  
    30.         (*root)->left=NULL;  
    31.         (*root)->right=NULL;  
    32.         (*root)->data=key;  
    33.         return;  
    34.     }  
    35.     else  
    36.     {  
    37.         if(Img)  
    38.         {  
    39.             if((*root)->data<=key)  
    40.                 InsertNode(&((*root)->left),key,Img);  
    41.             else  
    42.                 InsertNode(&((*root)->right),key,Img);  
    43.         }  
    44.         else  
    45.         {  
    46.             if((*root)->data>key)  
    47.                 InsertNode(&((*root)->left),key,Img);  
    48.             else  
    49.                 InsertNode(&((*root)->right),key,Img);  
    50.         }  
    51.     }  
    52. }  
    53.   
    54. void CreateBSTree(Node **root,int keys[],int len,bool Img)  
    55. {  
    56.     for(int i=0;i<len;i++)  
    57.         InsertNode(root,keys[i],Img);  
    58. }  
    59.   
    60. void PreOrderTraverse(Node *root,bool Img)  
    61. {  
    62.     if(root==NULL)  
    63.         return ;  
    64.     if(Img)  
    65.         PreOrderImg[index++]=root->data;  
    66.     else  
    67.         PreOrder[index++]=root->data;  
    68.     if(root->left)  
    69.         PreOrderTraverse(root->left,Img);  
    70.     if(root->right)  
    71.         PreOrderTraverse(root->right,Img);  
    72. }  
    73.   
    74. bool IsSame(int keys[],int len,bool Img)  
    75. {  
    76.     int i;  
    77.     if(Img)  
    78.     {  
    79.         for(i=0;i<len;i++)  
    80.         {  
    81.             if(keys[i]!=PreOrderImg[i])  
    82.                 return false;  
    83.         }  
    84.         return true;  
    85.     }  
    86.     else  
    87.     {  
    88.         for(i=0;i<len;i++)  
    89.         {  
    90.             if(keys[i]!=PreOrder[i])  
    91.                 return false;  
    92.         }  
    93.         return true;  
    94.     }  
    95. }  
    96.   
    97. void PostOrderTraverse(Node *root)  
    98. {  
    99.     if(root==NULL)  
    100.         return;  
    101.     if(root->left)  
    102.         PostOrderTraverse(root->left);  
    103.     if(root->right)  
    104.         PostOrderTraverse(root->right);  
    105.     PostOrder[index++]=root->data;  
    106. }  
    107.   
    108. void Display(int len)  
    109. {  
    110.     int i;  
    111.     for(i=0;i<len;i++)  
    112.     {  
    113.         if(i==0)  
    114.             printf("%d",PostOrder[i]);  
    115.         else  
    116.             printf(" %d",PostOrder[i]);  
    117.     }  
    118.     printf("\n");  
    119. }  
    120.   
    121. void Destroy(Node **root)  
    122. {  
    123.     if((*root)->left==NULL&&(*root)->right==NULL)  
    124.     {  
    125.         free(*root);  
    126.         *root=NULL;  
    127.     }  
    128.     else if((*root)->left)  
    129.         Destroy(&((*root)->left));  
    130.     else if((*root)->right)  
    131.         Destroy(&((*root)->right));  
    132. }  
    133.   
    134. int main(int argc,char *argv[])  
    135. {  
    136.     int i,n;  
    137.     Node *root=NULL;//这两个初始化必须得有,否则会出现段错误;在自己的机器  
    138.     Node *rootImg=NULL;  //上可以运行,但提交后就段错误,估计是服务器端  
    139.     scanf("%d",&n);      //编译器的问题吧  
    140.     for(i=0;i<n;i++)  
    141.         scanf("%d",&Keys[i]);  
    142.     CreateBSTree(&root,Keys,n,false);  
    143.     CreateBSTree(&rootImg,Keys,n,true);  
    144.     index=0;  
    145.     PreOrderTraverse(root,false);  
    146.     if(IsSame(Keys,n,false))  
    147.     {  
    148.         printf("YES\n");  
    149.         index=0;  
    150.         PostOrderTraverse(root);  
    151.         Display(n);  
    152.     }  
    153.     else  
    154.     {  
    155.         index=0;  
    156.         PreOrderTraverse(rootImg,true);  
    157.         if(IsSame(Keys,n,true))  
    158.         {  
    159.             printf("YES\n");  
    160.             index=0;  
    161.             PostOrderTraverse(rootImg);  
    162.             Display(n);  
    163.         }  
    164.         else  
    165.         {  
    166.             printf("NO\n");  
    167.         }  
    168.     }  
    169.     Destroy(&root);  
    170.     Destroy(&rootImg);  
    171.   
    172.     return 0; 

Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree),布布扣,bubuko.com

Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)

标签:des   style   blog   class   code   c   

原文地址:http://www.cnblogs.com/wangqingnong/p/3728659.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!