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

#include<iostream>
#include<vector>
#include<string>
#include<sstream>
using namespace std;
vector<int> in,post;
struct node{
	int key,level,father;
	int lchild=-1,rchild=-1;
};
node t[1111];
//flag=0表示树为满树
bool flag=0;
void create(int &r,int il,int ir,int pl,int pr,int level,int father){
	int i=il;
	while(in[i]!=post[pr-1]) ++i;
	r=in[i];
	t[r].father=father;
	t[r].level=level;
	int cnt=0;
	if(il<i){
		create(t[r].lchild,il,i,pl,pl+i-il,level+1,r);
		++cnt;
	}
	if(i+1<ir){
		create(t[r].rchild,i+1,ir,pl+i-il,pr-1,level+1,r);
		++cnt;
	}
	//如果当前节点有且仅有1个子节点,则树不是满树
	if(cnt==1) flag=1;
}
int main(){
	int n,m;
	cin>>n;
	in.resize(n);
	post.resize(n);
	for(int i=0;i<n;++i)
        cin>>post[i];
    for(int i=0;i<n;++i)
        cin>>in[i];
	int root;
	create(root,0,n,0,n,1,-1);
	cin>>m;
	getchar();
	while(m--){
		string s;
		getline(cin,s);
		//注意istringstream的用法
		istringstream is(s);
		is>>s;
		if(s=="It"){
			cout<<(flag?"No":"Yes")<<endl;
		}else{
			int x=stoi(s);
			is>>s;
			int y;
			if(s=="and"){
				is>>y;
				is>>s;
				is>>s;
				if(s=="on"){
					cout<<(t[x].level!=t[y].level?"No":"Yes")<<endl;
				}else if(s=="siblings"){
					cout<<((t[x].father!=t[y].father||x==y)?"No":"Yes")<<endl;
				}
			}else{
				is>>s;
				is>>s;
				if(s=="root"){
					cout<<(x!=root?"No":"Yes")<<endl;
				}else if(s=="parent"){
					is>>s;
					is>>y;
					cout<<(t[y].father!=x?"No":"Yes")<<endl;
				}else if(s=="left"){
					is>>s;
					is>>s;
					is>>y;
					cout<<(t[y].lchild!=x?"No":"Yes")<<endl;
				}else if(s=="right"){
					is>>s;
					is>>s;
					is>>y;
					cout<<(t[y].rchild!=x?"No":"Yes")<<endl;
				}
			}
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值