7-4 Structure of a Binary Tree (30 分)

要判断给出的点是否出现过,不然即使在同一层或者是兄弟节点也是无效的。

#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <cstdio>
#include <cctype>
#include <unordered_map>
#include <map>
using namespace std;
const int N = 35;
int post[N], in[N];
unordered_map<int, int> mp, pm;
typedef pair<int, string> PII;
int tag;
struct tree{
	int val;
	tree* left, *right;
	tree(int x){
		val = x;
		left = right = nullptr;
	}
};
tree* create(int pl,int pr, int inl, int inr, int ind){
	if(pl > pr) return nullptr;
	tree* root = new tree(post[pr]);
	pm[post[pr]] = ind;
	int k = mp[post[pr]];
	int leftnum = k - inl;
	root->left = create(pl, pl+leftnum-1, inl, k -1, 2*ind);
	root->right = create(pl+leftnum, pr-1, k+1, inr, 2*ind+1);
	return root;
}
void dfs(tree* root){
	if(!root) return;
	if(!root->left&&root->right) tag = 1;
	if(root->left&&!root->right) tag = 1;
	dfs(root->left);
	dfs(root->right);
}
int main() {
	int n;
	cin>>n;
	for(int i = 0; i < n; i++){
		cin>>post[i];
	}
	for(int i = 0; i < n; i++){
		cin>>in[i];
		mp[in[i]] = i;
	}
	tree* root = create(0, n-1, 0, n-1, 1);
	int m;
	tag = 0;
	dfs(root);
	cin>>m;
	string str, s;
	while(m--){
		cin>>str;
		if(str == "It"){
			cin>>str>>str>>str>>str;
			if(!tag) cout<<"Yes"<<endl;
			else cout<<"No"<<endl;
		}else{
			int a = stoi(str);
			cin>>str;
			if(str == "and"){
				int b;
				cin>>b;
				cin>>str>>str;
				if(str == "on"){
					cin>>str>>str>>str;
					if(pm.count(a)&&pm.count(b)){
						int u = pm[a], v = pm[b];
						int lvu = log(u)/log(2);
						int lvv = log(v)/log(2);
						if(lvu == lvv) cout<<"Yes"<<endl;
						else cout<<"No"<<endl;
					}else cout<<"No"<<endl;
				}else{
					if(pm.count(a)&&pm.count(b)){
						int u = pm[a], v = pm[b];
						if(u/2 == v/2) cout<<"Yes"<<endl;
						else cout<<"No"<<endl;
					}else cout<<"No"<<endl;
				}
			}else{
				cin>>str>>str;
				if(str=="root"){
					if(pm.count(a)){
						int u = pm[a];
						if(u==1) cout<<"Yes"<<endl;
						else cout<<"No"<<endl;
					}else cout<<"No"<<endl;
				}else if(str=="left"){
					cin>>str>>str;
					int b;
					cin>>b;
					if(pm.count(a)&&pm.count(b)){
						int u = pm[a], v = pm[b];
						if(u == 2*v) cout<<"Yes"<<endl;
						else cout<<"No"<<endl;
					}else cout<<"No"<<endl;
				}else if(str == "right"){
					cin>>str>>str;
					int b;
					cin>>b;
					if(pm.count(a)&&pm.count(b)){
						int u = pm[a], v = pm[b];
						if(u == 2*v+1) cout<<"Yes"<<endl;
						else cout<<"No"<<endl;
					}else cout<<"No"<<endl;
				}else{
					cin>>str;
					int b;
					cin>>b;
					if(pm.count(a)&&pm.count(b)){
						int u = pm[a], v = pm[b];
						if(u == v/2) cout<<"Yes"<<endl;
						else cout<<"No"<<endl;
					}else cout<<"No"<<endl;
				}
			}
		}
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值