天梯赛 L3 二叉搜索树的结构

这篇博客探讨了天梯赛L3级别的二叉搜索树问题,详细介绍了如何构建二叉搜索树,并通过阅读陈述句进行有效判断。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:点击打开链接

思路:题目不难,就是麻烦一些,创建完二叉树后,读取陈述句判断即可。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
int data[105],le[105],rig[105];
int fa[105],level[105];
int root;
map<int,int> m;
void add(int &r,int father,int depth,int loc){
	if(r == -1){
		r = loc;
		fa[loc] = father;
		level[loc] = depth;
		return;
	}
	if(data[loc] < data[r]){
		add(le[r],r,depth + 1,loc);
	}
	else{
		add(rig[r],r,depth + 1,loc);
	}
}
int main(){
	int n;
	scanf("%d",&n);
	root = -1;
	memset(le,-1,sizeof(le));
	memset(rig,-1,sizeof(rig));
	for(int i = 1;i <= n;i++){
		scanf("%d",&data[i]);
		add(root,-1,1,i);
		m[data[i]] = i;
	}
	int q,a,b;
	string aa;
	bool flag;
	scanf("%d",&q);
	while(q--){
		flag = false;
		scanf("%d",&a);
		cin>>aa;
		if(aa == "is"){
			cin>>aa>>aa;
			if(aa == "root"){
				if(m.find(a) != m.end() && data[root] == a) flag = true;
			}
			else if(aa == "parent"){
				cin >> aa;
				cin >> b;
				if(m.find(a) != m.end() && m.find(b) != m.end() && fa[m[b]] == m[a]) flag = true;
			}
			else if(aa == "left"){
				cin >> aa >> aa;
				cin >> b;
				if(m.find(a) != m.end() && m.find(b) != m.end() && fa[m[a]] == m[b] && a < b) flag = true;
			}
			else{
				cin >> aa >> aa;
				cin >> b;
				if(m.find(a) != m.end() && m.find(b) != m.end() && fa[m[a]] == m[b] && a > b) flag = true;
			}		
		}
		else{
			cin >> b;
			cin >> aa >> aa;
			if(aa == "siblings"){
				if(m.find(a) != m.end() && m.find(b) != m.end() && fa[m[a]] == fa[m[b]]) flag = true;
			}
			else{
				cin >> aa >> aa >> aa;
				if(m.find(a) != m.end() && m.find(b) != m.end() && level[m[a]] == level[m[b]]) flag = true;
			}
		}
		puts(flag?"Yes":"No");
	}
	return 0;
}

edition 2:

#include <bits/stdc++.h>
using namespace std;
int root;
int data[105],le[105],rig[105],fa[105],level[105];
map<int,int> m;
void add(int &r,int f,int loc){
	if(r == -1){
		r = loc;
		fa[r] = f;
		level[r] = level[f] + 1;
	}
	else if(data[loc] < data[r]){
		add(le[r],r,loc);
	}
	else{
		add(rig[r],r,loc);
	}
}
int main(){
	int n;
	cin >> n;
	root = -1;
	memset(le,-1,sizeof(le));
	memset(rig,-1,sizeof(rig));
	level[101] = 0;
	for(int i = 0;i < n;i++){
		cin >> data[i];
		m[data[i]] = i;
		add(root,101,i);
	}
	int mm;
	cin >> mm;
	while(mm--){
		int a,b;
		string te;
		int flag = false;
		cin >> a >> te;
		if(te == "is"){
			cin >> te >> te;
			if(te == "root"){
				if(m.find(a) != m.end() && m[a] == root){
					flag = true;
				}
			}
			else if(te == "parent"){
				cin >> te >> b;
				if(m.find(a) != m.end() && m.find(b) != m.end() && fa[m[b]] == m[a]){
					flag = true;
				}
			}
			else if(te == "left"){
				cin >> te >> te >> b;
				if(m.find(a) != m.end() && m.find(b) != m.end() && fa[m[a]] == m[b] && a < b){
					flag = true;
				}
			}
			else if(te == "right"){
				cin >> te >> te >> b;
				if(m.find(a) != m.end() && m.find(b) != m.end() && fa[m[a]] == m[b] && a > b){
					flag = true;
				}
			}
		}
		else if(te == "and"){
			cin >> b >> te >> te;
			if(te == "siblings"){
				if(m.find(a) != m.end() && m.find(b) != m.end() && fa[m[a]] == fa[m[b]]){
					flag = true;
				}
			}
			else if(te == "on"){
				cin >> te >> te >> te;
				if(m.find(a) != m.end() && m.find(b) != m.end() && level[m[a]] == level[m[b]]){
					flag = true;
				}
			}
		}
		puts(flag?"Yes":"No");
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值