【PAT】1043 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 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.对输入的一段序列生成一棵二叉搜索树

  2.判断:

      a.此二叉树的先序遍历和输入的序列相等,则输出此二叉树的后序遍历序列

      b.在 a 不成立的情况下,求这棵二叉树的镜像先序遍历序列,若和输入的序列相等,则输出二叉树的镜像后序遍历序列

      c.以上都不成立时,输出 "NO"

C++代码如下:

 1 #include<iostream>
 2 #include<vector>
 3 using namespace std;
 4 #define maxn 1005
 5 struct Node {
 6     int data;
 7     Node *lchild=NULL, *rchild=NULL;
 8 };
 9 
10 void insert(Node* &root, int x) {
11     if (root == NULL) {
12         root = new Node;
13         root->data = x;
14         return;
15     }
16     if (x >= root->data) {
17         insert(root->rchild, x);
18     }
19     else
20         insert(root->lchild, x);
21     return;
22 }
23 
24 Node* createBST(const vector<int> v) {
25     Node* root=NULL;
26     
27     for (int i = 0; i < v.size(); i++) {
28         insert(root, v[i]);
29     }
30     return root;
31 }
32 
33 vector<int>pre, pre_mirr, post,post_mirr;
34 void preorder(Node*root) {
35     if (root == NULL) return;
36     pre.push_back(root->data);
37     preorder(root->lchild);
38     preorder(root->rchild);
39 }
40 
41 void mirrBST(Node*root) {
42     if (root == NULL)return;
43     pre_mirr.push_back(root->data);
44     mirrBST(root->rchild);
45     mirrBST(root->lchild);
46 }
47 
48 void postorder(Node*root) {
49     if (root == NULL)return;
50     postorder(root->lchild);
51     postorder(root->rchild);
52     post.push_back(root->data);
53 }
54 
55 void postmirrorder(Node*root) {
56     if (root == NULL)return;
57     postmirrorder(root->rchild);
58     postmirrorder(root->lchild);
59     post_mirr.push_back(root->data);
60 }
61 int main() {
62     int n;
63     int temp;
64     cin >> n;
65     vector<int>v;
66     for (int i = 0; i < n; i++) {
67         cin >> temp;
68         v.push_back(temp);
69     }
70     Node*root = createBST(v);
71     preorder(root);
72     mirrBST(root);
73     if (v == pre) {
74         postorder(root);
75         cout <<"YES"<<endl<< post[0];
76         for (int i = 1; i < post.size(); i++)
77             cout <<' '<< post[i];
78         cout << endl;    
79     }
80     else if (v == pre_mirr) {
81         postmirrorder(root);
82         cout << "YES" << endl << post_mirr[0];
83         for (int i = 1; i < post_mirr.size(); i++)
84             cout << ' ' << post_mirr[i];
85         cout << endl;
86     }
87     else cout << "NO" << endl;
88     return 0;
89 }

 

posted on 2018-08-26 19:21  Pink.Pig  阅读(245)  评论(0编辑  收藏  举报