1043. Is It a Binary Search Tree (25)
2016-02-14 11:51
441 查看
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:
Sample Output 1:
Sample Input 2:
Sample Output 2:
Sample Input 3:
Sample Output 3:
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
#include <iostream> #include <algorithm> using namespace std; const int maxn = 1005; struct node{ int l,r,d; node(int dd = 0,int ll = -1,int rr = -1):d(dd),l(ll),r(rr){} }; node T[maxn]; int preorder[maxn],inorder[maxn]; int n,kase,id; int buildtree(int x1 ,int y1,int x2,int y2,int tag){ if(y1 - x1 == y2 - x2 && y1 - x1 > 0){ int idx = -1; for(int i=0;i<y1-x1;i++)if(preorder[x1] == inorder[x2+i]){ idx = i; if(tag == 0)break; } if(idx == -1)return -2; int t = id++; if(idx != -1)T[t].d = preorder[x1]; int tl = buildtree(x1+1,x1+idx+1,x2,x2+idx,tag),tr = buildtree(x1+idx+1,y1,x2+idx+1,y2,tag); T[t].l =tl; T[t].r = tr; return ((tl == -2)||(tr == -2))?-2:t; } else return -1; } void postorder(int x){ if(x != -1){ postorder(T[x].l); postorder(T[x].r); if(kase++)cout<<" "; cout<<T[x].d; } } int main(){ cin>>n; for(int i=0;i<n;i++){cin>>preorder[i];inorder[i] = preorder[i];} sort(inorder,inorder+n); int root; if((root = buildtree(0,n,0,n,0)) != -2){ id = 0; cout<<"YES\n"; postorder(root); }else{ id = 0; sort(inorder,inorder+n,greater<int>()); if((root = buildtree(0,n,0,n,1)) != -2){ cout<<"YES\n"; postorder(root); }else cout<<"NO\n"; } return 0; }
相关文章推荐
- libdvbpsi源码分析(四)PAT表解析/重建
- PAT配置
- 什么是端口复用动态地址转换(PAT) 介绍配置实例
- MikroTik layer7-protocol
- PAT是如何工作的
- PAT 乙级题:1002. 写出这个数 (20)
- PAT (Advanced Level) Practise 1001-1010
- 数据结构学习与实验指导(一)
- PAT Basic Level 1001-1010解题报告
- 1001. 害死人不偿命的(3n+1)猜想
- 1002. 写出这个数
- 1032. 挖掘机技术哪家强
- 1001. 害死人不偿命的(3n+1)猜想 (PAT basic)
- 1002. 写出这个数(PAT Basic)
- 1004. 成绩排名(PAT Basic)
- 1006. 换个格式输出整数(PAT Basic)
- 1007. 素数对猜想(PAT Basic)
- 1008. 数组元素循环右移问题
- 1009. 说反话(PAT Basic)
- 1011. A+B和C(PAT Basic)