PAT1043 Is It a Binary Search Tree
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
1 #include<iostream> 2 #include<stdlib.h> 3 #include<vector> 4 #include<stack> 5 #include<algorithm> 6 7 using namespace std; 8 9 enum Tags{Left, Right}; 10 typedef struct node{ 11 int val; 12 node *left, *right; 13 }BSTNode; 14 15 typedef struct stackElem{ 16 BSTNode *p; 17 Tags flag; 18 }StackElem; 19 20 static const int MAX = 1000; 21 int n, data[MAX]; 22 vector<int> pre, post; 23 24 BSTNode *create(int e){ 25 BSTNode *t = (BSTNode*)malloc(sizeof(BSTNode)); 26 t->val = e; 27 t->left = t->right = NULL; 28 return t; 29 } 30 31 void Insert(BSTNode* &t, int e){ 32 if(t==NULL){ 33 t = create(e); 34 return; 35 } 36 else if(t->val>e){ 37 Insert(t->left, e); 38 } 39 else{ 40 Insert(t->right, e); 41 } 42 } 43 44 BSTNode *buildBSTree(){ 45 BSTNode *root = NULL; 46 for(int i=0;i<n;i++){ 47 Insert(root, data[i]); 48 } 49 return root; 50 } 51 52 void PreOrder(BSTNode* &t){ 53 if(t!=NULL){ 54 pre.push_back(t->val); 55 PreOrder(t->left); 56 PreOrder(t->right); 57 } 58 } 59 60 void PostOrder(BSTNode* &t){ 61 if(t!=NULL){ 62 PostOrder(t->left); 63 PostOrder(t->right); 64 post.push_back(t->val); 65 } 66 } 67 68 void PostOrdered(BSTNode* &t){ 69 StackElem se; 70 stack<StackElem> s; 71 BSTNode *p; 72 p = t; 73 int num = 0; 74 if(p==NULL){ 75 return; 76 } 77 while(p!=NULL||!s.empty()){ 78 while(p!=NULL){ 79 se.flag = Left; 80 se.p = p; 81 s.push(se); 82 p = p->left; 83 } 84 se = s.top(); 85 s.pop(); 86 p = se.p; 87 if(num==n-1){ 88 cout<<p->val; 89 p = NULL; 90 } 91 else{ 92 if(se.flag==Left){ 93 se.flag = Right; 94 s.push(se); 95 p = p->right; 96 } 97 else{ 98 num++; 99 cout<<p->val<<" "; 100 p = NULL; 101 } 102 } 103 } 104 cout<<endl; 105 } 106 107 void mPostOrder(BSTNode* &t){ 108 StackElem se; 109 stack<StackElem> s; 110 BSTNode *p; 111 p = t; 112 int num = 0; 113 if(p==NULL){ 114 return; 115 } 116 while(p!=NULL||!s.empty()){ 117 while(p!=NULL){ 118 se.flag = Right; 119 se.p = p; 120 s.push(se); 121 p = p->right; 122 } 123 se = s.top(); 124 s.pop(); 125 p = se.p; 126 if(num==n-1){ 127 cout<<p->val; 128 p = NULL; 129 } 130 else{ 131 if(se.flag==Right){ 132 se.flag = Left; 133 s.push(se); 134 p = p->left; 135 } 136 else{ 137 num++; 138 cout<<p->val<<" "; 139 p = NULL; 140 } 141 } 142 } 143 cout<<endl; 144 } 145 146 int main(){ 147 cin>>n; 148 for(int i=0;i<n;i++){ 149 cin>>data[i]; 150 } 151 BSTNode *tree = buildBSTree(); 152 PreOrder(tree); 153 PostOrder(tree); 154 reverse(post.begin(), post.end()); 155 int pcnt = 0; 156 int mcnt = 0; 157 for(int i=0;i<n;i++){ 158 if(pre[i]==data[i]){ 159 pcnt++; 160 } 161 if(post[i]==data[i]){ 162 mcnt++; 163 } 164 } 165 if(pcnt==n){ 166 cout<<"YES"<<endl; 167 PostOrdered(tree); 168 } 169 else if(mcnt==n){ 170 cout<<"YES"<<endl; 171 mPostOrder(tree); 172 } 173 else{ 174 cout<<"NO"<<endl; 175 } 176 return 0; 177 }