Source:
Description:
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 Imageof 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 (≤). 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, orNO
if not. Then if the answer isYES
, 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
Keys:
- 二叉查找树(Binary Search Tree)
- 二叉树的建立
- 二叉树的遍历
Code:
1 /* 2 Data: 2019-06-26 17:25:17 3 Problem: PAT_A1043#Is It a Binary Search Tree 4 AC: 26:46 5 6 题目大意: 7 BST定义:lchild < root <= rchild 8 镜像树:交换左右子树 9 现给定一个序列,判断其是否为BST或Mirror BST的先序遍历,若是,打印后序遍历 10 11 基本思路: 12 由先序序列可以构建一棵唯一的二叉树, 13 分别NLR和NRL遍历该二叉树,若遍历序列与给定序列相同,则Yes,反之No 14 */ 15 #include<cstdio> 16 #include<vector> 17 using namespace std; 18 vector<int> pre,post,input; 19 struct node 20 { 21 int data; 22 node *lchild,*rchild; 23 }; 24 25 void Insert(node *&root, int x) 26 { 27 if(root == NULL) 28 { 29 root = new node; 30 root->data = x; 31 root->lchild = root->rchild = NULL; 32 } 33 else if(x < root->data) 34 Insert(root->lchild, x); 35 else 36 Insert(root->rchild, x); 37 } 38 39 void NLR(node *root) 40 { 41 if(root == NULL) 42 return; 43 pre.push_back(root->data); 44 NLR(root->lchild); 45 NLR(root->rchild); 46 post.push_back(root->data); 47 } 48 49 void NRL(node *root) 50 { 51 if(root == NULL) 52 return; 53 pre.push_back(root->data); 54 NRL(root->rchild); 55 NRL(root->lchild); 56 post.push_back(root->data); 57 } 58 int main() 59 { 60 #ifdef ONLINE_JUDGE 61 #else 62 freopen("Test.txt", "r", stdin); 63 #endif // ONLINE_JUDGE 64 65 int n,x; 66 scanf("%d", &n); 67 node *root = NULL; 68 for(int i=0; i<n; i++) 69 { 70 scanf("%d", &x); 71 Insert(root, x); 72 input.push_back(x); 73 } 74 NLR(root); 75 if(pre == input) 76 { 77 printf("YES\n"); 78 for(int i=0; i<post.size(); i++) 79 printf("%d%c", post[i],i==post.size()-1?'\n':' '); 80 } 81 else 82 { 83 pre.clear(); 84 post.clear(); 85 NRL(root); 86 if(pre == input) 87 { 88 printf("YES\n"); 89 for(int i=0; i<post.size(); i++) 90 printf("%d%c", post[i],i==post.size()-1?'\n':' '); 91 } 92 else 93 printf("NO\n"); 94 } 95 96 return 0; 97 }