1043. Is It a Binary Search Tree (25)

来源:互联网 发布:qt多进程编程 编辑:程序博客网 时间:2024/06/02 05:41

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:
78 6 5 7 10 8 11
Sample Output 1:
YES5 7 6 8 11 10 8
Sample Input 2:
78 10 11 8 6 7 5
Sample Output 2:
YES11 8 10 7 5 6 8
Sample Input 3:
78 6 8 5 10 9 11
Sample Output 3:
NO
#include <iostream>#include <vector>#include <map>#include <queue>#include <algorithm>#include <cstdio>#include <cstdlib>using namespace std;enum type{BST,BSTMIR,OTHER};struct Node{Node(int n,Node* l = NULL,Node* r = NULL):num(n),left(l),right(r){}int num;Node* left;Node* right;};bool isbstpre(const vector<int>& v,int a,int b){if (a > b)return true;int idx;for (idx = a + 1;idx <= b;++ idx)if (v[idx] >= v[a])break;for (int i = idx + 1;i <= b;++ i)if (v[i] < v[a])return false;if (isbstpre(v,a + 1,idx - 1) && isbstpre(v,idx + 1,b))return true;elsereturn false;}bool isbstmirpre(const vector<int>& v,int a,int b){if (a > b)return true;int idx;for (idx = a + 1;idx <= b;++ idx)if (v[idx] < v[a])break;for (int i = idx + 1;i <= b;++ i)if (v[i] >= v[a])return false;if (isbstmirpre(v,a + 1,idx - 1) && isbstmirpre(v,idx + 1,b))return true;elsereturn false;}type gettype(const vector<int>& v){if (isbstpre(v,0,v.size() - 1))return BST;else if(isbstmirpre(v,0,v.size() - 1))return BSTMIR;elsereturn OTHER;}Node* getbst(vector<int>& v,int a,int b){if (a > b)return NULL;int idx;for (idx = a + 1;idx <= b;++ idx)if (v[idx] >= v[a])break;Node* root = new Node(v[a],getbst(v,a + 1,idx - 1),getbst(v,idx,b));return root;}Node* getbstmir(vector<int>& v,int a,int b){if (a > b)return NULL;int idx;for (idx = a + 1;idx <= b;++ idx){if (v[idx] < v[a])break;}Node* root = new Node(v[a],getbstmir(v,a + 1,idx - 1),getbstmir(v,idx,b));return root;}Node* gettreeroot(vector<int>& v){type t = gettype(v);switch(t){case BST:return getbst(v,0,v.size() - 1);case BSTMIR:return getbstmir(v,0,v.size() - 1);case OTHER:return NULL;}}void posttravel(Node* root,Node* last){if (root == NULL)return;posttravel(root->left,last);posttravel(root->right,last);cout << root->num;if (root != last)cout << " ";}int main(){freopen("input.txt","r",stdin);int n;cin >> n;vector<int> v;for(int i = 0;i < n;++ i){int tmp;cin >> tmp;v.push_back(tmp);}Node* root = gettreeroot(v);if (root == NULL){cout << "NO" << endl;}else{cout << "YES" << endl;posttravel(root,root);}}


原创粉丝点击