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:
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
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043.Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- PAT A 1043. Is It a Binary Search Tree (25)
- PAT-A-1043. Is It a Binary Search Tree (25)
- pat-a 1043. Is It a Binary Search Tree (25)
- Is It a Binary Search Tree (25)
- 1043. Is It a Binary Search Tree (25)
- 1043. Is It a Binary Search Tree (25)—PAT
- JavaScript小知识点总结
- hdoj 1213 How Many Tables
- Android Security 汇总
- JavaScript与MySQL交互发现ActiveXObject只有IE能用
- Kafka图文详解
- 1043. Is It a Binary Search Tree (25)
- Oracle数据库 表空间及用户的创建
- [递归]poj 1057 FILE MAPPING
- java中日期格式转换
- springMVC之Controller方法的返回值
- PL/SQL异常处理
- php二分查找法
- 二叉排序树——理解与实现
- ios学习资料