排序二叉树 HDOJ 5444 Elven Postman

news/2024/7/3 7:58:06

 

 

题目传送门

题意:给出线性排列的树,第一个数字是根节点,后面的数如果当前点小或相等往左走,否则往右走,查询一些点走的路径

分析:题意略晦涩,其实就是排序二叉树!1<<1000 普通数组开不下,用指针省内存。将树倒过来好理解些

E---------------------------------------W
                    2
                   / \
                  1   4
                      /
                     3

                     6
                    /
                   5
                  /
                 4
                / 
               3
              /
             2
            /
           1
E---------------------------------------W

收获:排序二叉树插入和查询

 

代码:

/************************************************
* Author        :Running_Time
* Created Time  :2015/9/14 星期一 15:34:35
* File Name     :H.cpp
 ************************************************/
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std;

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 1e3 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int a[N], q[N];
struct BST {
    int val;
    BST *left, *right;
    BST *insert(BST *p, int x) {
        if (p == NULL)  {
            BST *t = new BST;
            t->val = x;
            t->left = t->right = NULL;
            return t;
        }
        if (x <= p->val)    p->left = insert (p->left, x);
        else    p->right = insert (p->right, x);
        return p;
    }
    bool find(BST *p, int x)   {
        if (x == p->val)    return true;
        else if (p == NULL) return false;
        else if (x <= p->val)    {
            cout << "E";
            return find (p->left, x);
        }
        else    {
            cout << "W";
            return find (p->right, x);
        }
    }
}bst;

int main(void)    {
    ios::sync_with_stdio (false);
    int T;  cin >> T;
    while (T--) {
        int n;  cin >> n;
        for (int i=1; i<=n; ++i)    {
            cin >> a[i];
        }
        int m;  cin >> m;
        for (int i=1; i<=m; ++i)    {
            cin >> q[i];
        }
        BST *root = NULL;
        for (int i=1; i<=n; ++i)    {
            root = bst.insert (root, a[i]);
        }
        for (int i=1; i<=m; ++i)    {
            bst.find (root, q[i]);
            cout << endl;
        }
    }

    return 0;
}

  

转载于:https://www.cnblogs.com/Running-Time/p/4807643.html


http://www.niftyadmin.cn/n/4231761.html

相关文章

tomcat的8080,8009,8443,8005都是什么端口

<Server port"8005" shutdown"SHUTDOWN"> 远程停服务端口<Connector port"8080" protocol"HTTP/1.1" connectionTimeout"20000" redirectPort"8443" URIEncoding"UTF-8"/> 其中808…

Python之时间和日期使用小结

对于日期的操作可以说是比较常见的case了,日期与格式化字符串互转&#xff0c;日期与时间戳互转&#xff0c;日期的加减操作等&#xff0c;下面主要介绍下常见的需求场景如何实现 1. 基本包引入 主要需要引入时间和日期的处理包&#xff0c;后面的基本操作都是基于此 import …

Hibernate之deleted object would be re-saved by casc

2019独角兽企业重金招聘Python工程师标准>>> 在Hibernate中&#xff0c;删除存在关联关系的一个对象时&#xff0c;会出现 org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations)这个异常…

SpringBoot文件上传异常之提示The temporary upload location xxx is not valid

原文: 一灰灰Blog之Spring系列教程文件上传异常原理分析 SpringBoot搭建的应用&#xff0c;一直工作得好好的&#xff0c;突然发现上传文件失败&#xff0c;提示org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exc…

【转】【Flex】#010 操作XML文件(E4X)

该教程转载来自于&#xff1a;http://blog.chinaunix.net/uid-14767524-id-2785506.html 【看到这边文章的位置&#xff0c;具体原作者未知】 经过一些排版的修改&#xff0c;其他内容没变。【版权属于原作者】 一 在介绍Flex中操作XML之前&#xff0c;首先简单介绍下XML中…

代码随想录——376. 摆动序列

本题要考虑三种情况&#xff1a; 上下坡中有平坡数组首尾两端单调坡中有平坡 class Solution {public int wiggleMaxLength(int[] nums) {if(nums.length < 1) return nums.length;int curdiff 0;int prediff 0;int result 1;for(int i 1; i< nums.length;i){curdi…

SpringBoot文件上传异常之temporary upload location not valid

SpringBoot搭建的应用&#xff0c;一直工作得好好的&#xff0c;突然发现上传文件失败&#xff0c;提示org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.io.IOException: The temporary upload l…

200多个js技巧代码(四)

121.集中为按钮改变颜色<style>button{benc:expression(this.onfocus function(){this.style.backgroundColor#E5F0FF;})}</style><button>New</button>//122.判断是左键还是右键被按下<body οnmοusedοwnif(event.button1)alert("左键&quo…