当前位置: 首页 > news >正文

【GPLT 三阶题目集】L3-016 二叉搜索树的结构

二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;它的左、右子树也分别为二叉搜索树。(摘自百度百科)

给定一系列互不相等的整数,将它们顺次插入一棵初始为空的二叉搜索树,然后对结果树的结构进行描述。你需要能判断给定的描述是否正确。例如将{ 2 4 1 3 0 }插入后,得到一棵二叉搜索树,则陈述句如“2是树的根”、“1和4是兄弟结点”、“3和0在同一层上”(指自顶向下的深度相同)、“2是4的双亲结点”、“3是4的左孩子”都是正确的;而“4是2的左孩子”、“1和3是兄弟结点”都是不正确的。

输入格式:

输入在第一行给出一个正整数N(≤100),随后一行给出N个互不相同的整数,数字间以空格分隔,要求将之顺次插入一棵初始为空的二叉搜索树。之后给出一个正整数M(≤100),随后M行,每行给出一句待判断的陈述句。陈述句有以下6种:

A is the root,即"A是树的根";
A and B are siblings,即"A和B是兄弟结点";
A is the parent of B,即"A是B的双亲结点";
A is the left child of B,即"A是B的左孩子";
A is the right child of B,即"A是B的右孩子";
A and B are on the same level,即"A和B在同一层上"。
题目保证所有给定的整数都在整型范围内。

输出格式:

对每句陈述,如果正确则输出Yes,否则输出No,每句占一行。

输入样例:

5
2 4 1 3 0
8
2 is the root
1 and 4 are siblings
3 and 0 are on the same level
2 is the parent of 4
3 is the left child of 4
1 is the right child of 2
4 and 0 are on the same level
100 is the right child of 3

输出样例:

Yes
Yes
Yes
Yes
Yes
No
No
No

#include <iostream>
#include <string>
#include <string.h>
#include <map>
using namespace std;const int MAX = 1e7 + 10;
int tree[MAX]; //二叉搜索树
int deepth[MAX]; //结点深度
int tem;
map<int, int> num; //键值对应的结点编号void creatTree(int x, int d) //建立二叉搜索树
{if (tree[x] == 0x3f3f3f3f){tree[x] = tem;num.insert(make_pair(tem, x));deepth[x] = d;}else{if (tem < tree[x])creatTree(x * 2, d + 1);elsecreatTree(x * 2 + 1, d + 1);}return;
}int main()
{int n; cin >> n;memset(tree, 0x3f, sizeof(tree)); //将每个元素初始化为0x3f3f3f3ffor (int i = 0; i < n; i++){cin >> tem;creatTree(1, 1);}int k; cin >> k;while (k--){string str;int a, b;cin >> a >> str;if (str == "and"){cin >> b >> str >> str;if (str == "siblings"){if (num.find(a) == num.end() || num.find(b) == num.end())cout << "No" << endl;else if (num[a] / 2 == num[b] / 2)cout << "Yes" << endl;elsecout << "No" << endl;}else{getline(cin, str);if (num.find(a) == num.end() || num.find(b) == num.end())cout << "No" << endl;else if (deepth[num[a]] == deepth[num[b]])cout << "Yes" << endl;elsecout << "No" << endl;}}else{cin >> str >> str;if (str == "root"){if (num.find(a) == num.end())cout << "No" << endl;else if (num[a] == 1)cout << "Yes" << endl;elsecout << "No" << endl;}else if (str == "parent"){cin >> str >> b;if (num.find(a) == num.end() || num.find(b) == num.end())cout << "No" << endl;else if (num[a] == num[b] / 2)cout << "Yes" << endl;elsecout << "No" << endl;}else if (str == "left"){cin >> str >> str >> b;if (num.find(a) == num.end() || num.find(b) == num.end())cout << "No" << endl;else if (num[b] * 2 == num[a])cout << "Yes" << endl;elsecout << "No" << endl;}else{cin >> str >> str >> b;if (num.find(a) == num.end() || num.find(b) == num.end())cout << "No" << endl;else if (num[b] * 2 + 1 == num[a])cout << "Yes" << endl;elsecout << "No" << endl;}}}return 0;
}

 注意事项:

需要判断被询问的数据是否在树上,否则测试点2答案错误。

如有问题,欢迎提出。

http://www.lryc.cn/news/6060.html

相关文章:

  • 核心交换机安全多业务高性能万兆交换机
  • Android APK 签名打包原理分析(三)【静默安装的实现方案】
  • mulesoft MCIA 破釜沉舟备考 2023.02.14.05
  • 结构体的三种定义方法、结构体类型名(可选标志符)什么时候可以省略
  • cgo静态编译不能用glibc,用musl
  • ​力扣解法汇总1124. 表现良好的最长时间段
  • 12- 降维算法 (PCA降维/LDA分类/NMF) (数据处理)
  • QT+ OpenGL学习
  • C语言(字符串输入)
  • 背包问题求方案数(AcWing)(JAVA)
  • 一篇文章带你读懂HashMap
  • Java如何进行优雅的判空——Optional类的灵活应用
  • Fluent Python 笔记 第 12 章 继承的优缺点
  • Go语言读取解析yml文件,快速转换yml到go struct
  • 第二十六章 java并发常见知识内容(ThreadLocal 详解)
  • 人类的第一语言是什么
  • jsp(全部知识点)
  • 测试开发面试基础题
  • C++——多态|虚函数|重写|虚表
  • IPV4地址详解
  • (一)初识Streamlit(附安装)
  • 【新】华为OD机试 - 斗地主 2(Python)| 刷完获取OD招聘渠道
  • 秒杀项目之消息推送
  • 【重磅】IEEE33配电网两阶段鲁棒优化调度CCG
  • GPT2代码拆解+生成实例
  • 基于android的即时通讯APP 聊天APP
  • 【C++】二叉树之力扣经典题目1——详解二叉树的递归遍历,二叉树的层次遍历
  • MySQL数据库调优————SQL性能分析
  • sql数据库高级编程总结(一)
  • 软件工程(5)--喷泉模型