PAT A1043. Is It a Binary Search Tree (25)
来源:互联网 发布:金融数据录入 编辑:程序博客网 时间:2024/06/05 14:45
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 11Sample Output 1:
YES5 7 6 8 11 10 8Sample Input 2:
78 10 11 8 6 7 5Sample Output 2:
YES11 8 10 7 5 6 8Sample Input 3:
78 6 8 5 10 9 11Sample Output 3:
NO
#include<cstdio>#include<vector>#include<algorithm>#define Max 1010using namespace std;struct node{ int data; node* lchild; node* rchild;};void insert(node* &root,int x){if(root==NULL){root=new node;root->data = x;root->lchild=root->rchild=NULL;return;}if(x<root->data)insert(root->lchild,x);else insert(root->rchild,x);}void PreOrder(node * root,vector<int>&vi){if(root==NULL)return ;vi.push_back (root->data);PreOrder(root->lchild,vi);PreOrder(root->rchild,vi);}void PreOrderM(node * root,vector<int>&vi){if(root==NULL)return ;vi.push_back (root->data);PreOrderM(root->rchild,vi);PreOrderM(root->lchild,vi);}void PostOrder(node * root,vector<int>&vi){if(root==NULL)return;PostOrder(root->lchild,vi);PostOrder(root->rchild,vi);vi.push_back(root->data);}void PostOrderM(node * root,vector<int>&vi){if(root==NULL)return;PostOrderM(root->rchild,vi);PostOrderM(root->lchild,vi);vi.push_back(root->data);}int main(){vector<int> Or,Pre,PreM,Post,PostM;int m,n;scanf("%d",&n);node* q =NULL;for(int i=0;i<n;i++){scanf("%d",&m);Or.push_back(m); insert(q,m);}PreOrder(q,Pre);PreOrderM(q,PreM);PostOrder(q,Post);PostOrderM(q,PostM);if(Or==Pre){printf("YES\n");for(int i=0;i<Post.size();i++){printf("%d",Post[i]);if(i<Post.size()-1){printf(" ");}}}else if(Or==PreM){printf("YES\n");for(int i=0;i<PostM.size();i++){printf("%d",PostM[i]);if(i<PostM.size()-1){printf(" ");}}}else printf("NO\n");system("pause");return 0;}
- 【PAT】A1043. Is It a Binary Search Tree (25)
- PAT A1043. Is It a Binary Search Tree (25)
- PAT A1043 is it a binary search tree (25)
- A1043. 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)
- pat-a 1043. Is It a Binary Search Tree (25)
- 1043. Is It a Binary Search Tree (25)—PAT
- 【PAT】1043. Is It a Binary Search Tree (25)
- 浙大 PAT 1043. Is It a Binary Search Tree (25)
- PAT 1043. Is It a Binary Search Tree (25)
- PAT (Advanced) 1043. Is It a Binary Search Tree (25)
- 【PAT】1043. Is It a Binary Search Tree (25)
- PAT 1043. Is It a Binary Search Tree (25)
- pat 1043. Is It a Binary Search Tree (25)
- PAT 1043. Is It a Binary Search Tree (25)
- pat 1043. Is It a Binary Search Tree (25)
- PAT 1032Is It a Binary Search Tree (25)
- 【Leetcode】217. Contains Duplicate
- 蓝桥-算法-队列
- HTML5之SVG 2D入门4—笔画与填充
- 数据挖掘-diabetes数据集分析-糖尿病病情预测_线性回归_最小平方回归
- SpringMVC学习笔记(一)使用IntelliJ IDEA开发Spring MVC HelloWorld
- PAT A1043. Is It a Binary Search Tree (25)
- HTML5之SVG 2D入门5—颜色的表示及定义方式
- Python的字符串的引号
- session深度解析
- hduoj1216
- HTML5之SVG 2D入门6—视窗坐标系与用户坐标系及变换概述
- HTML5之SVG 2D入门7—SVG元素的重用与引用
- nginx, apache, mysql, php源码安装
- 根据中序和前序遍历重建二叉树