1043 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 (≤). 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

题意:

  给出一个数字序列,问这个序列是不是一棵BST或者Mirror BST前序遍历的结果,如果是则输出对应树的后序遍历结果,如果不是则输出NO。

思路:

  施工中………………

Code:

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 int n;
 6 vector<int> v;
 7 vector<int> post;
 8 
 9 typedef struct Node* node;
10 
11 struct Node {
12     int val;
13     node left;
14     node right;
15     Node(int v) {
16         val = v;
17         left = NULL;
18         right = NULL;
19     }
20 };
21 
22 bool isBST() {
23     bool found1 = false;
24     for (int i = 1; i < n; ++i) {
25         if (v[i] >= v[0]) found1 = true;
26         if (found1 && v[i] < v[0]) return false;
27     }
28     return true;
29 }
30 
31 bool isMIBST() {
32     bool found2 = false;
33     for (int i = 1; i < n; ++i) {
34         if (v[i] < v[0]) found2 = true;
35         if (found2 && v[i] >= v[0]) return false;
36     }
37     return true;
38 }
39 
40 node buildTree(int s, int e, bool isBST) {
41     if (s > e) return NULL;
42     node root = new Node(v[s]);
43     int pos = -1;
44     if (isBST) {
45         for (int i = s + 1; i <= e; ++i)
46             if (v[i] >= v[s]) {
47                 pos = i;
48                 break;
49             }
50     } else {
51         for (int i = s + 1; i <= e; ++i)
52             if (v[i] < v[s]) {
53                 pos = i;
54                 break;
55             }
56     }
57     if (pos == -1) return root;
58     root->left = buildTree(s + 1, pos - 1, isBST);
59     root->right = buildTree(pos, e, isBST);
60     return root;
61 }
62 
63 void postOrder(node root) {
64     if (root == NULL) return;
65     postOrder(root->left);
66     postOrder(root->right);
67     post.push_back(root->val);
68 }
69 
70 int main() {
71     cin >> n;
72     v.resize(n + 1);
73     for (int i = 0; i < n; ++i) cin >> v[i];
74     node root;
75     if (isBST()) {
76         cout << "YES" << endl;
77         root = buildTree(0, n - 1, true);
78     } else if (isMIBST()) {
79         cout << "YES" << endl;
80         root = buildTree(0, n - 1, false);
81     } else {
82         cout << "NO" << endl;
83         return 0;
84     }
85     postOrder(root);
86     bool isFirst = true;
87     for (int i : post) {
88         if (isFirst) {
89             cout << i;
90             isFirst = false;
91         } else {
92             cout << " " << i;
93         }
94     }
95     cout << endl;
96     return 0;
97 }

注意:

  上面的代码有三组数据不能通过,有时间再更正…………

原文地址:https://www.cnblogs.com/h-hkai/p/12865952.html