PAT2019春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.

思路

先已知后续和中序,建立二叉树,结构体中设置左孩子、右孩子、层数、父结点四个成员。建立二叉树有一个小技巧,将这个结点的数据作为下标,就不会出现下标重叠的情况,判断时直接用下标也很方便。根据这7种字符串输入的规律先设计一个input函数判断输入的是哪种类型,将其中出现的数据用a和b存储下来,再根据结构体里的成员进行判断。

代码 

//来源https://blog.csdn.net/lianwaiyuwusheng/article/details/88084378
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
const int maxn=35;
int n,m,post[maxn],in[maxn],cnt=0,a,b,flag=0;
string str;
struct node{
	int depth,lchild,rchild,father;
}arr[maxn];
int create(int postr,int inl,int inr,int depth,int father){
	if(inl>inr) return -1;
	int i=inl;
	while(in[i]!=post[postr]) i++;
	arr[post[postr]].depth=depth;
	arr[post[postr]].father=father;
	arr[post[postr]].lchild=create(postr-inr+i-1,inl,i-1,depth+1,post[postr]);
	arr[post[postr]].rchild=create(postr-1,i+1,inr,depth+1,post[postr]);
	if((arr[post[postr]].rchild!=-1&&arr[post[postr]].lchild==-1)||(arr[post[postr]].rchild==-1&&arr[post[postr]].lchild!=-1))
		flag=1;
	return post[postr];
}
int input(){
	cin>>str;
	int i;
	if(str[0]>='0'&&str[0]<='9'){
		i=0;
		a=0;
		while(i<str.length())
			a=a*10+str[i++]-'0';
		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(){
	scanf("%d",&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=create(n-1,0,n-1,0,-1);
	scanf("%d",&m);
	for(int i=0;i<m;i++){
		int x=input();
		bool f=true;
		if(x==1){
			if(a!=root) f=false;
		}else if(x==2){
			if(arr[a].father!=arr[b].father) f=false;
		}else if(x==3){
			if(arr[b].father!=a) f=false;
		}else if(x==4){
			if(arr[b].lchild!=a) f=false;
		}else if(x==5){
			if(arr[b].rchild!=a) f=false;
		}else if(x==6){
			if(arr[a].depth!=arr[b].depth) f=false;
		}else if(x==7){
			if(flag) f=false;
		}
		if(f) printf("Yes\n");
		else printf("No\n");
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值