PAT L3-016 二叉搜索树的结构

时间:2023-03-09 15:32:18
PAT L3-016 二叉搜索树的结构

https://pintia.cn/problem-sets/994805046380707840/problems/994805047903240192

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

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

输入格式:

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

  • A is the root,即"A是树的根";
  • A and B are siblings,即"AB是兄弟结点";
  • A is the parent of B,即"AB的双亲结点";
  • A is the left child of B,即"AB的左孩子";
  • A is the right child of B,即"AB的右孩子";
  • A and B are on the same level,即"AB在同一层上"。

题目保证所有给定的整数都在整型范围内。

输出格式:

对每句陈述,如果正确则输出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 <bits/stdc++.h>
using namespace std; int N, Q, root = 0; struct Node{
long long val;
int l = -1;
int r = -1;
int fa = -1;
int lev = 0;
}node[110]; map<long long, int> mp, vis; void BuildBST(int root, int x) {
if(node[root].val > node[x].val) {
if(node[root].l == -1) {
node[root].l = x;
node[x].fa = root;
node[x].lev = node[root].lev + 1;
} else BuildBST(node[root].l, x);
} else if(node[root].val <= node[x].val) {
if(node[root].r == -1) {
node[root].r = x;
node[x].fa = root;
node[x].lev = node[root].lev + 1;
} else BuildBST(node[root].r, x);
}
} long long getnum(string s) {
long long ans = 0;
int len = s.length();
bool flag = false;
for(int i = 0; i < len; i ++) {
if(s[i] == '-') {
flag = true;
break;
}
}
for(int i = 0; i < len; i ++) {
if(s[i] >= '0' && s[i] <= '9') {
ans = ans * 10 + (s[i] - '0');
}
} if(flag) ans *= (-1);
return ans;
} int main() {
scanf("%d", &N);
for(int i = 0; i < N; i ++) {
long long x;
scanf("%lld", &x);
node[i].val = x;
vis[x] = 1;
mp[x] = i;
} for(int i = 1; i < N; i ++)
BuildBST(root, i); scanf("%d", &Q);
getchar();
while(Q --) {
long long num1, num2;
scanf("%lld", &num1);
string s;
getline(cin, s); if(s.find("root") != -1) {
if(num1 != node[root].val) printf("No\n");
else printf("Yes\n");
} else {
num2 = getnum(s);
if(vis[num1] == 0 || vis[num2] == 0) {
printf("No\n");
continue;
}
if(s.find("left") != -1) {
if(mp[num1] == node[mp[num2]].l) printf("Yes\n");
else printf("No\n");
} else if(s.find("right") != -1) {
if(mp[num1] == node[mp[num2]].r) printf("Yes\n");
else printf("No\n");
} else if(s.find("siblings") != -1) {
if(node[mp[num1]].fa == node[mp[num2]].fa) printf("Yes\n");
else printf("No\n");
} else if(s.find("parent") != -1) {
if(node[mp[num2]].fa == mp[num1]) printf("Yes\n");
else printf("No\n");
} else if(s.find("level") != -1) {
if(node[mp[num1]].lev == node[mp[num2]].lev) printf("Yes\n");
else printf("No\n");
}
//printf("%d\n", num2);
}
} return 0;
}