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;
}

 

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