zoukankan      html  css  js  c++  java
  • L3-016

    L3-016 二叉搜索树的结构 (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
    
    作者: 陈越
    单位: 浙江大学
    时间限制: 400 ms
    内存限制: 64 MB
    代码长度限制: 16 KB

    用指针实现的二叉树,map判断相等之前要记得判断是否存在。

    #include <bits/stdc++.h>
    using namespace std;
    const int inf = 0x3f3f3f3f;
    struct node {
        int val;
        node *left, *right;
        node(int _val): val(_val) {
            left = nullptr;
            right = nullptr;
        }
    };
    node* insert(int val, node* root) {
        if(root==NULL) {
            root = new node(val);
            return root;
        }
        if(val<(root->val)) root->left = insert(val, root->left);
        else root->right = insert(val, root->right);
        return root;
    }
    map<int,int> H;
    map<int,int> parent;
    map<int,int> leftson;
    map<int,int> rightson;
    
    void bfs(node* root) {
        queue<node*> q;
        H[root->val] = 1;
        q.push(root);
        while(!q.empty()) {
            node *now = q.front(); q.pop();
            if(now->left!=NULL) {
                node *lson = now->left;
                H[lson->val] = H[now->val]+1;
                parent[lson->val] = now->val;
                leftson[now->val] = lson->val;
                q.push(now->left);
            }
            if(now->right!=NULL) {
                node *rson = now->right;
                H[rson->val] = H[now->val]+1;
                parent[rson->val] = now->val;
                rightson[now->val] = rson->val;
                q.push(now->right);
            }
        }
    }
    int n, m;
    string s;
    
    int main() {
        scanf("%d", &n);
        node *root = new node(inf);
        for (int x, i=1; i<=n; ++i) {
            scanf("%d", &x);
            root = insert(x, root);
        }
        root = root->left;
        bfs(root);
        scanf("%d", &m);
        getchar();
        while(m--) {
            bool fg=false;
            getline(cin, s);
            int cnt = 0, x, y;
            for (int i=0; s[i]; ++i) if(s[i]==' ') ++cnt;
            if(cnt==3) {
                sscanf(s.c_str(), "%d", &x);
                fg = (root->val==x);
            } else if(cnt==4) {
                sscanf(s.c_str(), "%d and %d", &x, &y);
                fg = (parent.count(x)&&parent.count(y)&&parent[x]==parent[y]);
            } else if(cnt==5) {
                sscanf(s.c_str(), "%d is the parent of %d", &x, &y);
                fg = (parent.count(y)&&parent[y]==x);
            } else if(cnt==7) {
                sscanf(s.c_str(), "%d and %d", &x, &y);
                fg = (H.count(x) && H.count(y) && H[x]==H[y]);
            } else if(cnt==6) {
                char op[10];
                sscanf(s.c_str(), "%d is the %s child of %d", &x, op, &y);
                if(!strcmp(op, "left")) fg = (leftson.count(y) && leftson[y]==x);
                else fg = (rightson.count(y) && rightson[y]==x);
            }
            puts(fg?"Yes": "No");
        }
        return 0;
    }
  • 相关阅读:
    element ui 表单清空
    element ui 覆盖样式 方法
    element ui 修改表单值 提交无效
    element ui 抽屉里的表单输入框无法修改值
    element ui 抽屉首次显示 闪烁
    css 左侧高度 跟随右侧内容高度 自适应
    PICNUF框架
    elementui 抽屉组件标题 出现黑色边框
    vue 子组件跨多层调用父组件中方法
    vue 编辑table 数据 未点击提交,table里的数据就发生了改变(深拷贝处理)
  • 原文地址:https://www.cnblogs.com/acerkoo/p/10599423.html
Copyright © 2011-2022 走看看