zoukankan      html  css  js  c++  java
  • HDU 5444

    题意

    一些精灵住在一棵二叉搜索树上(???),给出一个序列表示这棵二叉搜索树(输入n个数的第一个数为根节点,第二个数和这个根节点比较大小,假如大于当前根节点的值,就往右边的节点走,小于往左边的节点走),现在邮递员要给他们送信,求送信每次走的方向。

    思路

    题目太长了 其实看懂了题意就能知道是个裸的二叉搜索树模板

    AC代码

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    using namespace std;
    
    struct node{
        int val;
        node *lch, *rch;
    };
    
    node *insert(node *p, int x){
        if(p==NULL){
            node *q = new node;
            q->val = x;
            q->lch = q->rch = NULL;
            return q;
        }
        else {
            if(x < p->val) p->lch = insert(p->lch, x);
            else p->rch = insert(p->rch, x);
            return p;
        }
    }
    
    void find(node *p, int x){
        if(p==NULL) return;
        else if(x==p->val) return;
        else if(x<p->val){
            printf("E");
            return find(p->lch, x);
        }
        else{
            printf("W");
            return find(p->rch, x);
        }
    }
    
    int main()
    {
        int T, q, qq, d, n;
        scanf("%d",&T);
        while(T--){
            scanf("%d",&n);
            node *root = NULL;
            for(int i = 0; i < n; i++){
                scanf("%d",&d);
                root = insert(root, d);
            }
            scanf("%d",&q);
            while(q--){
                scanf("%d",&qq);
                find(root, qq);
                printf("
    ");
            }
        }
    
        return 0;
    }
    
  • 相关阅读:
    Python语言之并发编程
    python语言之系统工具
    python语言之正则
    python语言之字符串与字节
    Python语言之持久化
    Python语言之数字格式化与时间
    Python语言之异常处理与测试
    Java-AQS源码详解(细节很多!)
    redis的主从复制原理
    Amdahl定律和可伸缩性
  • 原文地址:https://www.cnblogs.com/JinxiSui/p/9740521.html
Copyright © 2011-2022 走看看