1043. Is It a Binary Search Tree (25)

来源:互联网 发布:免费笑话api数据接口 编辑:程序博客网 时间:2024/06/03 17:44
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.


IDEA

1.根据给定序列建立二叉搜索树,建立节点,带插入序列比该节点值小放到左孩子,否则放到右孩子;

建立镜像二叉搜索树,建立节点,带插入序列比该节点值小放到右孩子,否则放到左孩子;

2.分别前序遍历二叉搜索树和镜像二叉搜索树,对不原给定序列,若都不相同则输出no

3.树的后序遍历问题


CODE

#include<iostream>#include<vector> #include<queue>#include<algorithm>#include<fstream>using namespace std;struct Node{int val;Node *left;Node *right;Node(int v):val(v),left(NULL),right(NULL){}};int n;Node *tree,*treeImg;Node *insert(Node *p,int val,int isImg){if(p==NULL){p=new Node(val);}else{if(isImg){if(val>=p->val){p->left=insert(p->left,val,isImg);//cout<<"p->val="<<p->val<<endl<<"p->left->val="<<p->left->val<<endl;}else{p->right=insert(p->right,val,isImg);//cout<<"p->val="<<p->val<<endl<<"p->right->val="<<p->right->val<<endl;}}else{if(val<p->val){p->left=insert(p->left,val,isImg);//cout<<"p->val="<<p->val<<endl<<"p->left->val="<<p->left->val<<endl;}else{p->right=insert(p->right,val,isImg);//cout<<"p->val="<<p->val<<endl<<"p->right->val="<<p->right->val<<endl;}}}return p;}void preOrder(Node *tree,vector<int> &vec){if(tree==NULL){return;}//cout<<tree->val<<"_";vec.push_back(tree->val);preOrder(tree->left,vec);preOrder(tree->right,vec);}void postOrder(Node *tree,vector<int> &vec){if(tree==NULL){return;}postOrder(tree->left,vec);postOrder(tree->right,vec);vec.push_back(tree->val);//cout<<tree->val<<"_";}int main(){#ifndef ONLINE_JUDGEfreopen("input.txt","r",stdin);#endifint n;while(cin>>n){vector<int> seq;for(int i=0;i<n;i++){int val;cin>>val;seq.push_back(val);}tree=NULL;treeImg=NULL;for(int i=0;i<n;i++){tree=insert(tree,seq[i],0);treeImg=insert(treeImg,seq[i],1);}vector<int> pre,preImg,post;preOrder(tree,pre);preOrder(treeImg,preImg);if(pre!=seq&&preImg!=seq){cout<<"NO"<<endl;}else{cout<<"YES"<<endl;if(pre==seq){postOrder(tree,post);}else if(preImg==seq){postOrder(treeImg,post);}vector<int>::iterator it;for(it=post.begin();it!=post.end();it++){if(it==post.begin()){cout<<*it;}else{cout<<" "<<*it;}}cout<<endl;} }#ifndef ONLINE_JUDGEfclose(stdin);#endifreturn 0;}


0 0
原创粉丝点击