A1043. 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<cstdio>
 2 #include<iostream>
 3 #include<vector>
 4 using namespace std;
 5 typedef struct NODE{
 6     struct NODE *lchild, *rchild;
 7     int key;
 8 }node;
 9 int N;
10 vector<int> keys, pre, preM, ans; 
11 void insert(node* &root, int key){
12     if(root == NULL){
13         root = new node;
14         root->key = key;
15         root->lchild = NULL;
16         root->rchild = NULL;
17         return;
18     }
19     if(key >= root->key){
20         insert(root->rchild, key);
21     }else{
22         insert(root->lchild, key);
23     }
24 }
25 node* create(vector<int> &keys){
26     node* root = NULL;
27     for(int i = 0; i < N; i++)
28         insert(root, keys[i]);
29     return root;
30 }
31 void preOrder(node* root){
32     if(root == NULL)
33         return;
34     pre.push_back(root->key);
35     preOrder(root->lchild);
36     preOrder(root->rchild);
37 }
38 void preOrder2(node* root){
39     if(root == NULL)
40         return;
41     preM.push_back(root->key);
42     preOrder2(root->rchild);
43     preOrder2(root->lchild);
44 }
45 void postOrder(node* root){
46     if(root == NULL)
47         return;
48     postOrder(root->lchild);
49     postOrder(root->rchild);
50     ans.push_back(root->key);
51 }
52 void postOrder2(node* root){
53     if(root == NULL)
54         return;
55     postOrder2(root->rchild);
56     postOrder2(root->lchild);
57     ans.push_back(root->key);
58 }
59 
60 int main(){
61     int temp;
62     scanf("%d", &N);
63     for(int i = 0; i < N; i++){
64         scanf("%d", &temp);
65         keys.push_back(temp);
66     }
67     node* root = create(keys);
68     preOrder(root);
69     preOrder2(root);
70     if(pre == keys ){
71         printf("YES\n");
72         postOrder(root);
73         for(int i = 0; i < N; i++){
74             if(i != N - 1)
75                 printf("%d ", ans[i]);
76             else printf("%d", ans[i]);
77         }
78     }else if(preM == keys){
79         printf("YES\n");
80         postOrder2(root);
81         for(int i = 0; i < N; i++){
82             if(i != N - 1)
83                 printf("%d ", ans[i]);
84             else printf("%d", ans[i]);
85         }
86     }else {
87         printf("NO\n");
88     }
89     cin >> N;
90     return 0;
91 }
View Code

 

总结:

1、题意:给出一组key,先按照给出的顺序建立搜索树。再对其本身和他的镜像进行先序遍历,看看是否先序遍历的序列和给出的一组key顺序相同。

2、对逆转的镜像树,可以不必实际上逆转该树,而仅仅在先序和后序访问左右子树时,从原来的先左后右变成先右后左

3、两个vector在元素为int时可以直接比较

4、注意字符串不要打错,“NO”打成“No”结果检查好久。

5、本题中允许搜索树中有重复的key,在定义中右子树大于等于根节点。  recursively:递归地

posted @ 2018-02-08 23:27  ZHUQW  阅读(115)  评论(0编辑  收藏  举报