PAT 1043. Is It a Binary Search Tree (25)
2018-03-18 00:59
423 查看
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:
NO
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <iostream>
using namespace std;
struct node
{
int data;
node* r;
node* l;
node():l(NULL),r(NULL){}
};
void insert(node* &root,int x)
{
if(!root)
{
root = (node*)malloc(sizeof(node));
root->data = x;
root->r = NULL;
root->l = NULL;
return;
}
else
{
if(x >= root->data) insert(root->r,x);
else if(x < root->data) insert(root->l,x);
}
}
int i = 0;
vector<int> v,vpre,vpost;
void preorder(node* root)
{
if(root)
{
vpre.push_back(root->data);
preorder(root->l);
preorder(root->r);
}
}
void Imagepreorder(node* root)
{
if(root)
{
vpre.push_back(root->data);
Imagepreorder(root->r);
Imagepreorder(root->l);
}
}
bool image = false;
void postorder(node* root)
{
if(root)
{
if(!image)
{
postorder(root->l);
postorder(root->r);
vpost.push_back(root->data);
}
else
{
postorder(root->r);
postorder(root->l);
vpost.push_back(root->data);
}
}
}
int main()
{
int n;
scanf("%d",&n);
node* root = NULL;
int x;
for(int i = 0; i < n; i++)
{
scanf("%d",&x);
v.push_back(x);
insert(root,x);
}
preorder(root);
for(int i = 0; i < n;i++)
{
if(vpre[i] != v[i])
{
image = true;
break;
}
}
vpre.clear();
Imagepreorder(root);
if(image)
{
for(int i = 0; i
4000
< n; i++)
{
if(vpre[i] != v[i])
{
cout<<"NO"<<endl;
return 0;
}
}
}
postorder(root);
cout<<"YES"<<endl;
for(int i = 0; i < n; i++)
{
if(i == 0)
{
cout<<vpost[i];
}
else
{
cout<<" "<<vpost[i];
}
}
cout<<endl;
return 0;
}
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 <cstdio>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <iostream>
using namespace std;
struct node
{
int data;
node* r;
node* l;
node():l(NULL),r(NULL){}
};
void insert(node* &root,int x)
{
if(!root)
{
root = (node*)malloc(sizeof(node));
root->data = x;
root->r = NULL;
root->l = NULL;
return;
}
else
{
if(x >= root->data) insert(root->r,x);
else if(x < root->data) insert(root->l,x);
}
}
int i = 0;
vector<int> v,vpre,vpost;
void preorder(node* root)
{
if(root)
{
vpre.push_back(root->data);
preorder(root->l);
preorder(root->r);
}
}
void Imagepreorder(node* root)
{
if(root)
{
vpre.push_back(root->data);
Imagepreorder(root->r);
Imagepreorder(root->l);
}
}
bool image = false;
void postorder(node* root)
{
if(root)
{
if(!image)
{
postorder(root->l);
postorder(root->r);
vpost.push_back(root->data);
}
else
{
postorder(root->r);
postorder(root->l);
vpost.push_back(root->data);
}
}
}
int main()
{
int n;
scanf("%d",&n);
node* root = NULL;
int x;
for(int i = 0; i < n; i++)
{
scanf("%d",&x);
v.push_back(x);
insert(root,x);
}
preorder(root);
for(int i = 0; i < n;i++)
{
if(vpre[i] != v[i])
{
image = true;
break;
}
}
vpre.clear();
Imagepreorder(root);
if(image)
{
for(int i = 0; i
4000
< n; i++)
{
if(vpre[i] != v[i])
{
cout<<"NO"<<endl;
return 0;
}
}
}
postorder(root);
cout<<"YES"<<endl;
for(int i = 0; i < n; i++)
{
if(i == 0)
{
cout<<vpost[i];
}
else
{
cout<<" "<<vpost[i];
}
}
cout<<endl;
return 0;
}
相关文章推荐
- PAT 1043. Is It a Binary Search Tree (25)
- PAT 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 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 (Advanced Level) Practise 1043 Is It a Binary Search Tree (25)
- PAT (Advanced Level) Practise 1043 Is It a Binary Search Tree (25)
- PAT 1043. Is It a Binary Search Tree (25)
- PAT Advanced Level 1043. Is It a Binary Search Tree (25)(Java and C++)
- PAT - 甲级 - 1043. Is It a Binary Search Tree (25)(递归建树,求后序,BST)
- 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)