团体程序设计天梯赛-2017总决赛 L3-1 二叉搜索树的结构

版权声明:while (!success) try(); https://blog.csdn.net/qq_35850147/article/details/88754573

L3-1 二叉搜索树的结构 (30 分)

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

给定一系列互不相等的整数,将它们顺次插入一棵初始为空的二叉搜索树,然后对结果树的结构进行描述。你需要能判断给定的描述是否正确。例如将{ 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,即"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;

struct node
{
    bool left, right;
    int il, ir, par, dep;
};
unordered_map<int, node> M;

void insert(int n, int r)
{
    M[n].dep++;
    if (n > r)
    {
        if (!M[r].right)
        {
            M[r].right = true;
            M[r].ir = n;
            M[n].par = r;
        }
        else
            insert(n, M[r].ir);
    }
    else
    {
        if (!M[r].left)
        {
            M[r].left = true;
            M[r].il = n;
            M[n].par = r;
        }
        else
            insert(n, M[r].il);
    }
}

int main()
{
    int n, m, root;
    scanf("%d", &n);
    scanf("%d", &root);
    while (--n)
    {
        int t;
        scanf("%d", &t);
        insert(t, root);
    }
    scanf("%d", &m);
    getchar();
    while (m--)
    {
        char s[100];
        fgets(s, 1000, stdin);
        if (strstr(s, "root"))
        {
            int a;
            sscanf(s, "%d", &a);
            printf("%s\n", a == root ? "Yes" : "No");
        }
        else if (strstr(s, "siblings"))
        {
            int a, b;
            sscanf(s, "%d%*[^0-9,-]%d", &a, &b);
            printf("%s\n", M.find(a) != M.end() && M.find(b) != M.end() && M[a].par == M[b].par && a != root && b != root ? "Yes" : "No");
        }
        else if (strstr(s, "parent"))
        {
            int a, b;
            sscanf(s, "%d%*[^0-9,-]%d", &a, &b);
            printf("%s\n", M.find(a) != M.end() && M.find(b) != M.end() && M[b].par == a && b != root ? "Yes" : "No");
        }
        else if (strstr(s, "left"))
        {
            int a, b;
            sscanf(s, "%d%*[^0-9,-]%d", &a, &b);
            printf("%s\n", M.find(a) != M.end() && M.find(b) != M.end() && M[b].left == true && M[b].il == a ? "Yes" : "No");
        }
        else if (strstr(s, "right"))
        {
            int a, b;
            sscanf(s, "%d%*[^0-9,-]%d", &a, &b);
            printf("%s\n", M.find(a) != M.end() && M.find(b) != M.end() && M[b].right == true && M[b].ir == a ? "Yes" : "No");
        }
        else
        {
            int a, b;
            sscanf(s, "%d%*[^0-9,-]%d", &a, &b);
            printf("%s\n", M.find(a) != M.end() && M.find(b) != M.end() && M[a].dep == M[b].dep ? "Yes" : "No");
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_35850147/article/details/88754573
今日推荐