L3-016. 二叉搜索树的结构
时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
陈越
二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;它的左、右子树也分别为二叉搜索树。(摘自百度百科)
给定一系列互不相等的整数,将它们顺次插入一棵初始为空的二叉搜索树,然后对结果树的结构进行描述。你需要能判断给定的描述是否正确。例如将{ 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 <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
int x[105][2];
int a[105];
int n, q,l,ll;
void dfs(int k, int kk, int level,int flag)
{
if (k == kk) { flag ? ll = level : l = level; return; }
if (x[k][0] != -1) dfs(x[k][0], kk, level + 1,flag);
if (flag&&ll) return;
if (!flag&&l) return;
if (x[k][1] != -1) dfs(x[k][1], kk, level + 1,flag);
}
int main()
{
while (~scanf("%d", &n))
{
memset(x, -1, sizeof x);
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
if (i == 1) continue;
int k = 1;
while (1)
{
if (a[i] > a[k] && x[k][1] != -1) k = x[k][1];
else if (a[i] < a[k] && x[k][0] != -1) k = x[k][0];
else
{
if (a[i] > a[k]) x[k][1] = i;
else x[k][0] = i;
break;
}
}
}
scanf("%d", &q);
while (q--)
{
int xx, yy, flag = 1, k=0, kk=0;
char ch[10], s[10];
scanf("%d%s", &xx, ch);
for (int i = 1; i <= n; i++)
{
if (xx == a[i])
{
k = i; break;
}
}
if (!k) flag = 0;
if (!strcmp(ch, "is"))
{
scanf("%s%s", ch, ch);
if (!strcmp(ch, "root"))
{
if (k != 1) flag = 0;
}
else if (!strcmp(ch, "parent"))
{
scanf("%s%d", ch, &yy);
for (int i = 1; i <= n; i++)
{
if (yy == a[i])
{
kk = i; break;
}
}
if (!kk) flag = 0;
if (x[k][0] != kk&&x[k][1] != kk) flag = 0;
}
else
{
scanf("%s%s%d", s, s, &yy);
for (int i = 1; i <= n; i++)
{
if (yy == a[i])
{
kk = i; break;
}
}
if (!kk) flag = 0;
if (!strcmp(ch, "left") && x[kk][0] != k) flag = 0;
if (!strcmp(ch, "right") && x[kk][1] != k) flag = 0;
}
}
else
{
scanf("%d%s%s", &yy, ch, ch);
for (int i = 1; i <= n; i++)
{
if (yy == a[i])
{
kk = i; break;
}
}
if (!kk) flag = 0;
if (!strcmp(ch, "siblings"))
{
flag = 0;
for (int i = 1; i <= n; i++)
if ((x[i][0] == k&&x[i][1] == kk) || (x[i][1] == k&&x[i][0] == kk)) flag = 1;
}
else
{
scanf("%s%s%s", ch, ch, ch);
l = 0, ll = 0;
dfs(1, k, 1,0);
dfs(1, kk, 1,1);
if (l != ll) flag = 0;
}
}
if (flag) printf("Yes\n");
else printf("No\n");
}
}
return 0;
}