L3-016 二叉搜索树的结构
Statement
Metadata
- 作者: 陈越
- 单位: 浙江大学
- 代码长度限制: 16 KB
- 时间限制: 400 ms
- 内存限制: 64 MB
二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;它的左、右子树也分别为二叉搜索树。(摘自百度百科)
给定一系列互不相等的整数,将它们顺次插入一棵初始为空的二叉搜索树,然后对结果树的结构进行描述。你需要能判断给定的描述是否正确。例如将{ 2 4 1 3 0 }插入后,得到一棵二叉搜索树,则陈述句如“2是树的根”、“1和4是兄弟结点”、“3和0在同一层上”(指自顶向下的深度相同)、“2是4的双亲结点”、“3是4的左孩子”都是正确的;而“4是2的左孩子”、“1和3是兄弟结点”都是不正确的。
输入格式
输入在第一行给出一个正整数
- 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
输出样例
Solution
#include <bits/stdc++.h>
using namespace std;
#define N 510
#define INF 0x3f3f3f3f
int n, a[N];
map<int, int> mp;
struct node {
    int fa, ls, rs, dep;
    node() {}
    node(int fa, int dep) : fa(fa), dep(dep) {
        ls = rs = -1;
    }
} t[N];
int ID(int x) {
    return mp[x];
}
void insert(int now, int x) {
    int &ls = t[now].ls, &rs = t[now].rs, dep = t[now].dep;
    if (a[x] > a[now]) {
        if (rs == -1) {
            rs = x;
            t[x] = node(now, dep + 1);
            return;
        }
        insert(rs, x);
    } else {
        if (ls == -1) {
            ls = x;
            t[x] = node(now, dep + 1);
            return;
        }
        insert(ls, x);
    }
}
int u, v;
void out(int vis) {
    if (ID(u) == 0 || ID(v) == 0)
        vis = 0;
    puts(vis ? "Yes" : "No");
}
int main() {
    mp.clear();
    scanf("%d", &n);
    scanf("%d", a + 1);
    t[1] = node(-1, 0);
    mp[a[1]] = 1;
    for (int i = 2; i <= n; ++i) {
        scanf("%d", a + i);
        mp[a[i]] = i;
        insert(1, i);
    }
    int q;
    cin >> q;
    char s[20];
    while (q--) {
        scanf("%d%s", &u, s);
        if (s[0] == 'i') {
            scanf("%s%s", s, s);
            if (s[1] == 'o') {
                if (ID(u) == 1)
                    puts("Yes");
                else
                    puts("No");
            } else if (s[0] == 'p') {
                scanf("%s%d", s, &v);
                out(t[ID(v)].fa == ID(u));
            } else if (s[0] == 'l') {
                scanf("%s%s%d", s, s, &v);
                out(t[ID(v)].ls == ID(u));
            } else {
                scanf("%s%s%d", s, s, &v);
                out(t[ID(v)].rs == ID(u));
            }
        } else {
            scanf("%d", &v);
            scanf("%s%s", s, s);
            // cout << s << endl;
            if (s[0] == 's')
                out(t[ID(u)].fa == t[ID(v)].fa);
            else {
                scanf("%s%s%s", s, s, s);
                //	cout << s << endl;
                out(t[ID(u)].dep == t[ID(v)].dep);
            }
        }
    }
    return 0;
}
  Last update: May 4, 2022