目录
1043 Is It a Binary Search Tree (25分)
1064 Complete Binary Search Tree (30分)
1099 Build A Binary Search Tree (30分)
1043 Is It a Binary Search Tree (25分)
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.
给定(二叉树/镜像树)的先序遍历的序列,(利用BST左小右大的性质)构造BST,求postorder
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
题意:给定⼀个整数序列,请你判断 这是否是对⼀棵⼆叉搜索树或其镜像进⾏前序遍历的结果。
分析:假设它是⼆叉搜索树,⼀开始isMirror为FALSE,根据⼆叉搜索树的性质将已知的前序转换为后序,转换过程中,如果发现最后输出的后序数组⻓度不为n,那就再设isMirror为true,然后清空后序数组,重新再转换⼀次(根据镜⾯⼆叉搜索树的性质),如果依旧转换后数组⼤⼩不等于n,就输出no 、否则输出yes、
#include <cstdio>
#include <vector>
using namespace std;
bool isMirror;
vector<int> pre, post;
void getpost(int root, int tail) {
if(root > tail) return ;
int i = root + 1, j = tail;
if(!isMirror) {
while(i <= tail && pre[root] > pre[i]) i++;
while(j > root && pre[root] <= pre[j]) j--;
} else {
while(i <= tail && pre[root] <= pre[i]) i++;
while(j > root && pre[root] > pre[j]) j--;
}
if(i - j != 1) return ;
getpost(root + 1, j);
getpost(i, tail);
post.push_back(pre[root]);
}
int main() {
int n;
scanf("%d", &n);
pre.resize(n);
for(int i = 0; i < n; i++)
scanf("%d", &pre[i]);
getpost(0, n - 1);
if(post.size() != n) {
isMirror = true;
post.clear();
getpost(0, n - 1);
}
if(post.size() == n) {
printf("YES\n%d", post[0]);
for(int i = 1; i < n; i++)
printf(" %d", post[i]);
} else {
printf("NO");
}
return 0;
}
1064 Complete Binary Search Tree (30分)
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.
A Complete Binary Tree (CBT) is a tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right.
Now given a sequence of distinct non-negative integer keys, a unique BST can be constructed if it is required that the tree must also be a CBT. You are supposed to output the level order traversal sequence of this BST.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤1000). Then N distinct non-negative integer keys are given in the next line. All the numbers in a line are separated by a space and are no greater than 2000.
Output Specification:
For each test case, print in one line the level order traversal sequence of the corresponding complete binary search 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:
10
1 2 3 4 5 6 7 8 9 0
Sample Output:
6 3 8 1 5 7 9 0 2 4
题意:给定一个CBT的树结构,和一个sequence(元素互不相同),把元素填进去,构成BST,输出层序遍历,
思路:开一个数组(存放BST的层次遍历),index从1开始,2*x是左节点;2*x+1是右节点;中序遍历一遍把数填进去
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include<map>
#include<cmath>
#define pb push_back
using namespace std;
const int maxn=100005;
int n,cnt=0;
int cbt[1005],num[1005];
void inorder(int root){
if(root>n) return ;
inorder(2*root);
cbt[root]=num[cnt++];
inorder(2*root+1);
}
int main(){
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&num[i]);
}
sort(num,num+n); //inorder sequence
inorder(1); //root=1 left=2*x right=2*x+1
for(int i=1;i<=n;i++){
printf("%d%c",cbt[i],i==n?'\n':' ');
}
return 0;
}
1099 Build A Binary Search Tree (30分)
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.
Given the structure of a binary tree and a sequence of distinct integer keys, there is only one way to fill (填充)these keys into the tree so that the resulting tree satisfies the definition of a BST. You are supposed to output the level order traversal sequence of that tree. The sample is illustrated by Figure 1 and 2.
题意:给定一个特定的树结构,和一个sequence(元素互不相同),把元素填进去,构成BST,输出层序遍历
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤100) which is the total number of nodes in the tree. The next N lines each contains the left and the right children of a node in the format left_index right_index
, provided that the nodes are numbered from 0 to N−1, and 0 is always the root. If one child is missing, then −1 will represent the NULL child pointer. Finally N distinct integer keys are given in the last line.
Output Specification:
For each test case, print in one line the level order traversal sequence of that tree. All the numbers must be separated by a space, with no extra space at the end of the line.
Sample Input:
9
1 6
2 3
-1 -1
-1 4
5 -1
-1 -1
7 -1
-1 8
-1 -1
73 45 11 58 82 25 67 38 42
Sample Output:
58 25 82 11 38 67 45 73 42
思路:和上题一样,构造静态二叉树,中序遍历二叉树把中序遍历地sequence填进去,然后bfs输出层序遍历
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<string>
#include<cstring>
#include<vector>
#include<map>
#include<cmath>
#define pb push_back
using namespace std;
const int maxn=105;
int n,cnt;
struct node{
int data;
int left,right;
}Node[maxn]; //二叉树的静态写法
int num[maxn];
void inorder(int root){ //中序遍历树,把数字填进去
if(root==-1) return ;
inorder(Node[root].left);
Node[root].data=num[cnt++];
inorder(Node[root].right);
}
void bfs(int root){
int count=0;
queue<int> qu;
qu.push(root);
while(!qu.empty()){
int tmp=qu.front();qu.pop();count++;
printf("%d%c",Node[tmp].data,count==n?'\n':' ');
if(Node[tmp].left!=-1)qu.push(Node[tmp].left);
if(Node[tmp].right!=-1)qu.push(Node[tmp].right);
}
}
int main(){
scanf("%d",&n);
int l,r;
for(int i=0;i<n;i++){
scanf("%d%d",&l,&r);
Node[i].left=l;
Node[i].right=r;
}
for(int i=0;i<n;i++){
scanf("%d",&num[i]);
}
sort(num,num+n); //中序序列
inorder(0);
bfs(0); //层次遍历输出
//printf("%d",Node[0].data);
return 0;
}