PAT 2019年春季 7-4 Structure of a Binary Tree (30 分)
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 103 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
实现思路:
题意给出后序中序建树,并且给一些语句,判断这些说法是否正确,这是一道stl的运用题,但是也有很多的坑点,就比如在判断A是否为B的父母的时候,别用parent[A]->data==B,因为有可能A的父节点是空的情况,就会出现空指针错误。最好的解决办法应该单独用一个map容器保存每个数值所对应的node结点指针,这样就只需要判断结点指针地址是否相等即可,还有本题运用sscanf(源字符串,"所要求的格式",&值),将所需要的int值单独取出,更加方便。
AC代码:
#include <iostream>
#include <unordered_map>
#include <cstring>
#include <queue>
using namespace std;
const int N=50;
int post[N],in[N],high[1010];
struct node {
int data,high;
node *l,*r;
};
unordered_map<int,node*> parent,mp;
node* build(int postL,int postR,int inL,int inR) {
if(postL>postR) return NULL;
node *root=new node;
root->data=post[postR];
mp[root->data]=root;
int k;
for(k=inL; k<=inR; k++) {
if(root->data==in[k]) break;
}
int leftNodeNum=k-inL;
root->l=build(postL,postL+leftNodeNum-1,inL,k-1);
if(root->l) parent[root->l->data]=root;
root->r=build(postL+leftNodeNum,postR-1,k+1,inR);
if(root->r) parent[root->r->data]=root;
return root;
}
bool bfs(node *root) {
queue<node*> q;
root->high=1;
q.push(root);
bool flag=true;
while(!q.empty()) {
node *root=q.front();
high[root->data]=root->high;
q.pop();
if((root->l==NULL&&root->r!=NULL)||(root->l!=NULL&&root->r==NULL)) flag=false;
if(root->l) {
root->l->high=root->high+1;
q.push(root->l);
}
if(root->r) {
q.push(root->r);
root->r->high=root->high+1;
}
}
return flag;
}
int main() {
cin.tie(0);
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]);
node *root=build(0,n-1,0,n-1);
cin>>m;
bool ans=bfs(root);
getchar();
while(m--) {
string str;
getline(cin,str);
int a,b;
bool tag=false;
if(str.find("root")!=string::npos) {
sscanf(str.c_str(),"%d is the root",&a);
if(a==root->data) tag=true;
} else if(str.find("siblings")!=string::npos) {
sscanf(str.c_str(),"%d and %d are siblings",&a,&b);
if(parent[a]==parent[b]) tag=true;
} else if(str.find("parent")!=string::npos) {
sscanf(str.c_str(),"%d is the parent of %d",&a,&b);
if(parent[b]==mp[a]) tag=true;
} else if(str.find("left")!=string::npos) {
sscanf(str.c_str(),"%d is the left child of %d",&a,&b);
if(mp[b]->l==mp[a]) tag=true;
} else if(str.find("right")!=string::npos) {
sscanf(str.c_str(),"%d is the right child of %d",&a,&b);
if(mp[b]->r==mp[a]) tag=true;
} else if(str.find("level")!=string::npos) {
sscanf(str.c_str(),"%d and %d are on the same level",&a,&b);
if(high[a]==high[b]) tag=true;
} else tag=ans;
if(tag) printf("Yes\n");
else printf("No\n");
}
return 0;
}

浙公网安备 33010602011771号