团体程序设计天梯赛-练习集 -- L3-016. 二叉搜索树的结构(模拟)

12 篇文章 0 订阅
10 篇文章 0 订阅

题意:

建立搜索二叉树,然后就是一系列询问了。

有几个坑:

要查询的数值可能不在二叉树上。

数值不是1~n的。需要以下标建立二叉树或者 离散化一下。


可能是时间紧迫问题,  差了四分 就是没想到 数值可能不在二叉树上了。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <unordered_map>
using namespace std;

const int maxn = 100 + 10;

unordered_map<int,int>mp;

int a[maxn];
int lch[maxn], rch[maxn],dep[maxn];
int fa[maxn];
int cnt = 1;
int root;

void dfs(int x, int d){
    if (x == -1) return ;

    dep[x] = d;
    dfs(lch[x],d+1);
    dfs(rch[x],d+1);
}
void insert(int x){
    int cur = 0;
    while(1){
        if (x < a[cur]){
            if (lch[cur] == -1){
                a[cnt] = x;
                fa[cnt] = cur;
                mp[x] = cnt;
                lch[cur] = cnt++;
                return;
            }
            else {
                fa[lch[cur] ] = cur;
                cur = lch[cur];
            }
        }
        else {
            if (rch[cur] == -1){
                a[cnt] = x;
                fa[cnt] = cur;
                mp[x] = cnt;
                rch[cur] = cnt++;
                return;
            }
            else {
                fa[rch[cur] ] = cur;
                cur = rch[cur];
            }
        }
    }
}
int find(int x){
    if (x== root) return 0;
    if (!mp.count(x)) return -1;
    return mp[x];
}

char s[1000];

int main(){
    int n,x;
    scanf("%d",&n);
    memset(lch,-1,sizeof lch);
    memset(rch,-1,sizeof rch);
    scanf("%d",&root);
    a[0] = root; fa[0] = -1;
    for (int i = 1; i < n; ++i){
        scanf("%d",&x);
        insert(x);
    }

    int q;
    scanf("%d",&q);

    dfs(0,0);
    int u,v;
    while(q--){
        scanf("%d",&u);
        scanf("%s",s);
        if (strcmp(s,"is") == 0){
            scanf("%s",s);
            scanf("%s",s);
//            cin >> s; cin >> s;
            if (!strcmp("root",s)){
                if (u == root) {
                    puts("Yes");
                }
                else puts("No");

            }
            else if (!strcmp(s,"parent")){
                scanf("%s",s); scanf("%d",&v);
                int pos = find(v);
                if (pos == -1) {
                    puts("No");
                    continue;
                }
                int fap = fa[pos];
                if (v == root || a[fap] != u){
                    puts("No");
                }
                else puts("Yes");
            }
            else if (!strcmp(s,"left")){
                scanf("%s",s);
                scanf("%s",s);
                scanf("%d",&v);
//                cin >> s; cin >> s; cin >>v;
                int pos = find(v);
                if (pos == -1) {
                    puts("No");
                    continue;

                }
                int lp = lch[pos];
                if (lp == -1 || a[lp] != u){
                    puts("No");
                }
                else puts("Yes");

            }
            else {
                scanf("%s",s);
                scanf("%s",s);
                scanf("%d",&v);
//                cin >> s; cin >> s; cin >>v;
                int pos = find(v);

                if (pos == -1){
                    puts("No");
                    continue;

                }

                int lp = rch[pos];
                if (lp == -1 || a[lp] != u){
                    puts("No");
                }
                else puts("Yes");

            }


        }
        else {
            scanf("%d",&v);
//            cin >>v;
            scanf("%s",s);
            scanf("%s",s);
//            cin >>s; cin >> s;
            if (!strcmp("on",s)){
                scanf("%s",s);
                scanf("%s",s);
                scanf("%s",s);
//                cin >> s ; cin >> s; cin >> s;
                int p1 = find(u);
                int p2 = find(v);
                if (p1 == -1||p2 == -1) {
                    puts("No");
                    continue;

                }
                if (dep[p1] != dep[p2]) puts("No");
                else puts("Yes");
            }
            else {
                int p1 = find(u);
                int p2 = find(v);


                if (p1 == -1 || p2 == -1){
                    puts("No");
                    continue;

                }
                if (fa[p1] != fa[p2]) puts("No");
                else puts("Yes");
            }
        }

    }

    return 0;
}



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

提交代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值