A1043. Is It a Binary Search Tree (25)

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

提交代码

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <iostream>
  4 #include <string.h>
  5 #include <math.h>
  6 #include <algorithm>
  7 #include <string>
  8 #include <stack> 
  9 #include <queue>
 10 using namespace std;
 11 int n; 
 12 struct tree{
 13     int data;
 14     tree *left;
 15     tree *right;
 16 };
 17 
 18 void  insert(tree *&root,int data)
 19 {
 20     if(root==NULL)
 21     {
 22      root=new tree;
 23      root->data=data;
 24      root->left=NULL;
 25      root->right=NULL;
 26      return ;    
 27     }
 28     if(data<root->data)insert(root->left,data);
 29     else insert(root->right,data);
 30 }
 31 
 32 void preorder(tree * root,vector<int> &pre)
 33 {
 34     if(root==NULL)return;
 35     pre.push_back(root->data);
 36     preorder(root->left,pre);
 37     preorder(root->right,pre);
 38     
 39 }
 40 void preorderM(tree * root,vector<int> &preM)
 41 {
 42     if(root==NULL)return ;
 43     preM.push_back(root->data);
 44     preorderM(root->right,preM);
 45     preorderM(root->left,preM);
 46     
 47 }
 48 void postorder(tree * root,vector<int> &post)
 49 {
 50     if(root==NULL)return ;
 51     postorder(root->left,post);
 52     postorder(root->right,post);
 53     post.push_back(root->data);    
 54 }
 55 void postorderM(tree * root,vector<int> &postM)
 56 {
 57     if(root==NULL)return;
 58     postorderM(root->right,postM);
 59     postorderM(root->left,postM);    
 60     postM.push_back(root->data);    
 61 }
 62 
 63 vector<int> origin,pre,preM,post,postM;
 64 int main(){
 65      scanf("%d",&n);
 66      tree * root=NULL;
 67      for(int i=0;i<n;i++)
 68      {
 69          int temp;
 70          scanf("%d",&temp);
 71          origin.push_back(temp);
 72          insert(root,temp);
 73      } 
 74      
 75     //树已经插入好,开始先序遍历
 76      preorder(root,pre); 
 77     //镜像先序遍历
 78      preorderM(root,preM) ;
 79      
 80      
 81      postorder(root,post);
 82      postorderM(root,postM);
 83      if(origin==pre)
 84      {
 85          printf("YES\n");
 86          for(int i=0;i<post.size();i++)
 87          {
 88              printf("%d",post[i]);
 89              if(i<post.size()-1)printf(" "); 
 90          }
 91      }else if(origin==preM)
 92      {
 93          printf("YES\n");
 94          for(int i=0;i<postM.size();i++)
 95          {
 96              printf("%d",postM[i]);
 97              if(i<postM.size()-1)printf(" "); 
 98          }
 99      }else
100      {
101          printf("NO\n");
102      }
103      
104      
105       
106     return 0;
107 }

 

posted @ 2015-03-06 22:18  Joilee  阅读(496)  评论(0编辑  收藏  举报