pat1043. Is It a Binary Search Tree (25)
1043. Is It a Binary Search Tree (25)
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 11Sample Output 1:
YES 5 7 6 8 11 10 8Sample Input 2:
7 8 10 11 8 6 7 5Sample Output 2:
YES 11 8 10 7 5 6 8Sample Input 3:
7 8 6 8 5 10 9 11Sample Output 3:
NO
1 #include<cstdio> 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<queue> 6 #include<vector> 7 #include<cmath> 8 #include<string> 9 #include<map> 10 #include<set> 11 using namespace std; 12 struct node 13 { 14 int v; 15 node *l,*r; 16 node() 17 { 18 l=r=NULL; 19 } 20 }; 21 bool Buildtree(node *&h,int *line,int n) 22 { 23 if(n==0) 24 { 25 return true; 26 } 27 int i; 28 h=new node(); 29 h->v=line[0]; 30 i=1; 31 while(i<n&&line[i]<line[0]) 32 { 33 i++; 34 } 35 int j=i; 36 while(j<n&&line[j]>=line[0]){ 37 j++; 38 } 39 if(j!=n){ 40 return false; 41 } 42 return Buildtree(h->l,line+1,i-1)&&Buildtree(h->r,line+i,n-i); 43 } 44 45 bool Buildtree1(node *&h,int *line,int n) 46 { 47 if(n==0) 48 { 49 return true; 50 } 51 int i; 52 h=new node(); 53 h->v=line[0]; 54 i=1; 55 while(i<n&&line[i]>=line[0]) 56 { 57 i++; 58 } 59 int j=i; 60 while(j<n&&line[j]<line[0]){ 61 j++; 62 } 63 if(j!=n){ 64 return false; 65 } 66 return Buildtree1(h->l,line+1,i-1)&&Buildtree1(h->r,line+i,n-i);//黏贴复制害死人 67 } 68 void Postorder(node *&h) 69 { 70 if(h) 71 { 72 Postorder(h->l); 73 Postorder(h->r); 74 printf("%d ",h->v); 75 delete []h; 76 } 77 } 78 int line[1005]; 79 int main() 80 { 81 //freopen("D:\\INPUT.txt","r",stdin); 82 int n; 83 while(scanf("%d",&n)!=EOF) 84 { 85 node *h; 86 int i; 87 for(i=0; i<n; i++) 88 { 89 scanf("%d",&line[i]); 90 } 91 if(n==1){ 92 printf("YES\n"); 93 printf("%d\n",line[0]); 94 continue; 95 } 96 if(line[0]>line[1]&&Buildtree(h,line,n))//BST 97 { 98 printf("YES\n"); 99 Postorder(h->l); 100 Postorder(h->r); 101 printf("%d\n",h->v); 102 continue; 103 } 104 if(line[0]<=line[1]&&Buildtree1(h,line,n)) 105 { 106 printf("YES\n"); 107 Postorder(h->l); 108 Postorder(h->r); 109 printf("%d\n",h->v); 110 continue; 111 } 112 printf("NO\n"); 113 } 114 return 0; 115 }