甲级PAT 2019年3月 Structure of a Binary Tree (30 分)

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.
  • 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

完整代码 

#include<bits/stdc++.h>
using namespace std;

struct Node{
	int data;
	int parent;
	int level;
	bool isleft;
}node[50];

int pre[50],in[50],post[50];
int num=0;
int n;

unordered_map<int,int> premap,childnum;
void pretra(int root,int start,int end,int level,int parent,bool isleft){
	if (start>end) return;
	int i=start;
	while(i<end && in[i]!=post[root]) i++;
	int pid=post[root];
	premap[pid]=num;
	node[num].level=level;
	node[num].parent=parent;
	node[num].isleft=isleft;
	num++;
	pretra(root-(end-i)-1,start,i-1,level+1,pid,true);
	pretra(root-1,i+1,end,level+1,pid,false);
}

bool issamelevel(int a,int b){
	int am=premap[a];
	int bm=premap[b];
	if(node[am].level==node[bm].level) return true;
	else return false;
}

bool issib(int a,int b){
	int am=premap[a];
	int bm=premap[b];
	if(node[am].parent==node[bm].parent) return true;
	else return false;
}

bool isfull(){
	for(int i=1;i<n;i++){
		int p=node[i].parent;
		childnum[p]++;
	}
	for(auto m:childnum){
		if(m.second==1) return false;
	}
	return true;
}

bool isp(int a,int b){
	int bnum=premap[b];
	if(a==node[bnum].parent) return true;
	else return false;
}

bool isl(int a,int b){
	int anum=premap[a];
	if(node[anum].parent==b &&node[anum].isleft ) return true;
	else return false;
}

bool isr(int a,int b){
	int anum=premap[a];
	if(node[anum].parent==b &&!node[anum].isleft ) return true;
	else return false;
}

bool isroot(int a){
	if(a==post[n-1]) return true;
	else return false;
}

int main(){
	int i,m,j;
	char cl[50],cr[50];
	cin>>n;
	char s[100];
	for(i=0;i<n;i++) cin>>post[i];
	for(i=0;i<n;i++) cin>>in[i];
	for(i=0;i<=n;i++){
		node[i].parent=-1;
		node[i].level=-1;
	}
	pretra(n-1,0,n-1,1,-1,false);
	cin>>m;
	getchar();
	bool isfulln=isfull();
	for(i=0;i<m;i++){
		memset(cl,0,sizeof(cl));
		memset(cr,0,sizeof(cr));
		int l=-1,r=-1;
		fgets(s,100,stdin);
		int lens=strlen(s);
		int index=0;
		
		for(j=0;j<lens;j++){
			if(s[j]==' ') break;
			cl[index++]=s[j];
		}
		l=atoi(cl);
		j++;
		if(s[j]=='a'){
			index=0;
			for(j=j+4;j<lens;j++){
				if(isdigit(s[j])) cr[index++]=s[j];
				else break;
			}
			r=atoi(cr);
			if(lens-j>15){
				if(issamelevel(l,r)) cout<<"Yes"<<endl;
				else cout<<"No"<<endl;
			}else{
				if(issib(l,r)) cout<<"Yes"<<endl;
				else cout<<"No"<<endl;
			}
			
		}else{
			j+=3;
			if(s[j]=='a'){
				if(isfulln) cout<<"Yes"<<endl;
				else cout<<"No"<<endl;
			}else{
				j+=4;
				//cout<<s[j]<<endl;
				if(s[j]=='p'){
					index=0;
					for(j=j+10;j<lens;j++){
						if(isdigit(s[j])) cr[index++]=s[j];
						else break;
					}
					r=atoi(cr);
					if(isp(l,r)) cout<<"Yes"<<endl;
					else cout<<"No"<<endl;
				}else if(s[j]=='l'){
					index=0;
					for(j=j+14;j<lens;j++){
						if(isdigit(s[j])) cr[index++]=s[j];
						else break;
					}
					r=atoi(cr);
					if(isl(l,r)) cout<<"Yes"<<endl;
					else cout<<"No"<<endl;
				}else if(s[j]=='r'){
					if(s[j+1]=='i'){
							index=0;
						for(j=j+15;j<lens;j++){
							if(isdigit(s[j])) cr[index++]=s[j];
							else break;
						}
						r=atoi(cr);
						if(isr(l,r)) cout<<"Yes"<<endl;
						else cout<<"No"<<endl;
					}else{
						if(isroot(l)) cout<<"Yes"<<endl;
						else cout<<"No"<<endl;
					}
				
				}
				
			}
			
			
		}
		
	}

	return 0;
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值