汇总贴
题目
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, a binary tree can be uniquely determined.
Now given a sequence of statements about the structure of the resulting tree, you are supposed to tell if they are correct or not. A statment is one of the following:
- A is the root
- A and B are siblings
- A is the parent of B
- A is the left child of B
- A is the right child of B
- A and B are on the same level
- It is a full tree
Note:
- Two nodes are on the same level, means that they have the same depth.
- A full binary tree is a tree in which every node other than the leaves has two children.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are no more than 10^3 and are separated by a space.
Then another positive integer M (≤30) is given, followed by M lines of statements. It is guaranteed that both A and B in the statements are in the tree.
Output Specification:
For each statement, print in a line Yes if it is correct, or No if not.
Sample Input:
9
16 7 11 32 28 2 23 8 15
16 23 7 32 11 2 28 15 8
7
15 is the root
8 and 2 are siblings
32 is the parent of 11
23 is the left child of 16
28 is the right child of 2
7 and 11 are on the same level
It is a full tree
Sample Output:
Yes
No
Yes
No
Yes
Yes
Yes
题目分析
1.postorder and inorder traversal sequences
给定后序和中序,这个是PAT甲级题库中很常见的知识点“二叉树的遍历”,可以参考《算法笔记》9.2节和题库的1086、1119、1138。
2.因为可能不是完全二叉树,同时输出的东西也挺复杂的,所以应该果断用DFS建二叉树。建树方法略有不同于1135(https://www.liuchuo.net/archives/4099),是采用map和结构体。
3.本题有7种输出,对应7个数据,都可以在DFS建树中体现:
(1)root,建树的返回就是root的值
(2)siblings,等价于两个节点的祖先是同一个
(3)parent,每个节点存父亲的值
(4)leftchild,A节点的左孩子是B,为了访问A的时候就能读出B节点的值,要用map和结构体
(5)rightchild,B节点的左孩子是A
(6)same level,记录节点的深度
(7)full tree,记录节点有没有左右孩子
满分代码
来源致谢:https://blog.csdn.net/lianwaiyuwusheng/article/details/88084378
#include<iostream>
#include<vector>
#include<cstdio>
#include<set>
#include<map>
#include<algorithm>
using namespace std;
#define MAX 40
struct node{
int l,r,d,p;
node(){}
node(int l,int r,int d,int p):l(l),r(r),d(d),p(p){}
};
map<int,node>arr;
int po[MAX],in[MAX],N,A,B;
bool flag=0;
string str;
int tree(int poR,int inL,int inR,int de,int pa)
{
if(inL>inR)return -1;
int i=inL;
while(in[i]!=po[poR])++i;
arr[po[poR]].p=pa;
arr[po[poR]].d=de;
arr[po[poR]].l=tree(poR-(inR-i)-1,inL,i-1,de+1,po[poR]);
arr[po[poR]].r=tree(poR-1,i+1,inR,de+1,po[poR]);
if((arr[po[poR]].r==-1&&arr[po[poR]].l!=-1)||(arr[po[poR]].r!=-1&&arr[po[poR]].l==-1))flag=1;
return po[poR];
}
int input()
{
int i;
cin>>str;
if(str[0]>='0'&&str[0]<='9'){
i=0;A=0;
while(i<str.length()){A=A*10+str[i]-'0';++i;}
cin>>str;
if(str=="and"){
scanf("%d",&B);
cin>>str;cin>>str;
if(str=="siblings"){
return 2;
}else{
i=3;
while(i--){cin>>str;}
return 6;
}
}else{
cin>>str;cin>>str;
if(str=="root")return 1;
else if(str=="parent"){
cin>>str;scanf("%d",&B);
return 3;
}else if(str=="left"){
cin>>str;cin>>str;scanf("%d",&B);
return 4;
}else if(str=="right"){
cin>>str;cin>>str;scanf("%d",&B);
return 5;
}
}
}else{
i=4;
while(i--)cin>>str;
return 7;
}
}
int main()
{
//freopen("test.txt","r",stdin);
scanf("%d",&N);
for(int i=0;i<N;++i)scanf("%d",&po[i]);
for(int i=0;i<N;++i)scanf("%d",&in[i]);
int root=tree(N-1,0,N-1,0,-1);
scanf("%d",&N);
while(N--){
int x=input();
bool f=0;
if(x==1){
if(A!=root)f=1;
}else if(x==2){
if(arr[A].p!=arr[B].p)f=1;
}else if(x==3){
if(arr[B].p!=A)f=1;
}else if(x==4){
if(arr[B].l!=A)f=1;
}else if(x==5){
if(arr[B].r!=A)f=1;
}else if(x==6){
if(arr[B].d!=arr[A].d)f=1;
}else if(x==7){
if(flag)f=1;
}
if(f)printf("No\n");
else printf("Yes\n");
}
return 0;
}