19年春季第四题 PAT甲级 1159 Structure of a Binary Tree(30分)

汇总贴

2020年3月PAT甲级满分必备刷题技巧

题目

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.
  • 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,记录节点有没有左右孩子

满分代码

#include<iostream>
#include<vector>
#include<cstdio>
#include<map>
using namespace std;
int post[maxn],in[maxn];
struct node{
    int l,r,depth,parent;
};
map<int,node> tree;
bool fullTree=true;
int build(int pe,int is,int ie,int dpt,int prt){
    if(is>ie)return -1;
    tree[post[pe]].depth=dpt;
    tree[post[pe]].parent=prt;
    int i=is;
    while(i<ie && in[i]!=post[pe])i++;
    tree[post[pe]].l=build(pe-ie+i-1,is,i-1,dpt+1,post[pe]);
    tree[post[pe]].r=build(pe-1,i+1,ie,dpt+1,post[pe]);
    if(tree[post[pe]].l==-1 && tree[post[pe]].r!=-1)fullTree=false;
    if(tree[post[pe]].l!=-1 && tree[post[pe]].r==-1)fullTree=false;
    return post[pe];
}
int main(){
    int n,m;
    cin>>n;
    for(int i=0;i<n;i++)scanf("%d",&post[i]);
    for(int i=0;i<n;i++)scanf("%d",&in[i]);
    int root=build(n-1, 0, n-1, 0, -1);
    scanf("%d\n",&m);
    string s1,s2,s3,s4,s5,s6,s7;
    for(int i=0;i<m;i++){
        bool yeah=false;
        cin>>s1>>s2;
        if(s2=="and"){
            cin>>s3>>s4>>s5;
            if(s5=="siblings"){
                if(tree[stoi(s1)].parent==tree[stoi(s3)].parent)yeah=true;
            }else{//same level
                if(tree[stoi(s1)].depth==tree[stoi(s3)].depth)yeah=true;
            }
        }else{
            cin>>s3>>s4;
            if(s4=="root"){
                if(root==stoi(s1))yeah=true;
            }else if(s4=="parent"){
                cin>>s5>>s6;
                if(tree[stoi(s6)].parent==stoi(s1))yeah=true;
            }else if(s4=="left"){
                cin>>s5>>s6>>s7;
                if(tree[stoi(s7)].r==stoi(s1))yeah=true;
            }else if(s4=="right"){
                cin>>s5>>s6>>s7;
                if(tree[stoi(s7)].r==stoi(s1))yeah=true;
            }else{//full tree
                if(fullTree)yeah=true;
            }
        }
        if(yeah)cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章