首页 > 编程语言 >LeetCode] 2476. Closest Nodes Queries in a Binary Search Tree

LeetCode] 2476. Closest Nodes Queries in a Binary Search Tree

时间:2024-02-26 13:47:44浏览次数:25  
标签:2476 Search set Closest val equal queries answer root

You are given the root of a binary search tree and an array queries of size n consisting of positive integers.

Find a 2D array answer of size n where answer[i] = [mini, maxi]:
mini is the largest value in the tree that is smaller than or equal to queries[i]. If a such value does not exist, add -1 instead.
maxi is the smallest value in the tree that is greater than or equal to queries[i]. If a such value does not exist, add -1 instead.
Return the array answer.

Example 1:
Example 1
Input: root = [6,2,13,1,4,9,15,null,null,null,null,null,null,14], queries = [2,5,16]
Output: [[2,2],[4,6],[15,-1]]
Explanation: We answer the queries in the following way:

  • The largest number that is smaller or equal than 2 in the tree is 2, and the smallest number that is greater or equal than 2 is still 2. So the answer for the first query is [2,2].
  • The largest number that is smaller or equal than 5 in the tree is 4, and the smallest number that is greater or equal than 5 is 6. So the answer for the second query is [4,6].
  • The largest number that is smaller or equal than 16 in the tree is 15, and the smallest number that is greater or equal than 16 does not exist. So the answer for the third query is [15,-1].

Example 2:
Example 2
Input: root = [4,null,9], queries = [3]
Output: [[-1,4]]
Explanation: The largest number that is smaller or equal to 3 in the tree does not exist, and the smallest number that is greater or equal to 3 is 4. So the answer for the query is [-1,4].

Constraints:
The number of nodes in the tree is in the range [2, 105].
1 <= Node.val <= 106
n == queries.length
1 <= n <= 105
1 <= queries[i] <= 106

二叉搜索树最近节点查询。

给你一个 二叉搜索树 的根节点 root ,和一个由正整数组成、长度为 n 的数组 queries 。
请你找出一个长度为 n 的 二维 答案数组 answer ,其中 answer[i] = [mini, maxi] :
mini 是树中小于等于 queries[i] 的 最大值 。如果不存在这样的值,则使用 -1 代替。
maxi 是树中大于等于 queries[i] 的 最小值 。如果不存在这样的值,则使用 -1 代替。
返回数组 answer 。

思路

这里我先提供一个次优解,思路是用treeset将二叉搜索树中的所有节点记录下来,然后利用 treeset 里的 ceiling 和 floor 两个特别的函数去分别找到比目标小的数字中的最大的和比目标大的数字中的最小的。

复杂度

时间O(nlogn) - 为什么有 nlogn 是因为 treeset 有排序的动作
空间O(n)

代码

Java实现

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<List<Integer>> closestNodes(TreeNode root, List<Integer> queries) {
        TreeSet<Integer> set = new TreeSet<>();
        helper(root, set);
        List<List<Integer>> res = new ArrayList<>();
        int n = queries.size();
        for (int i = 0; i < n; i++) {
            int val = queries.get(i);
            List<Integer> list = new ArrayList<>();
            if (set.floor(val) != null) {
                list.add(set.floor(val));
            } else {
                list.add(-1);
            }
            if (set.ceiling(val) != null) {
                list.add(set.ceiling(val));
            } else {
                list.add(-1);
            }
            res.add(list);
        }
        return res;
    }

    private void helper(TreeNode root, TreeSet<Integer> set) {
        if (root == null) {
            return;
        }
        set.add(root.val);
        helper(root.left, set);
        helper(root.right, set);
    }
}

标签:2476,Search,set,Closest,val,equal,queries,answer,root
From: https://www.cnblogs.com/cnoodle/p/18034150

相关文章

  • ElasticSearch集群搭建
    1环境和版本1.1操作系统干干静静的centos7系统,选取的是mini的iso最小化安装CentOSLinuxrelease7.9.2009(Core)1.2ElasticSearch版本本文使用的版本是8.11.3,下载地址:https://www.elastic.co/guide/en/elasticsearch/reference/current/install-elasticsearch.html1.3......
  • docker-compose 安装部署ElasticSearch 和 Kibana 8.8.1
    docker-compose安装部署ElasticSearch和Kibana8.8.1一、容器编排脚本(docker-compose.yml)version:"3.1"#服务配置services:elasticsearch:container_name:elasticsearch-8.8.1image:docker.elastic.co/elasticsearch/elasticsearch:8.8.1#用来给容......
  • Elasticsearch数据同步优化
    Elasticsearch数据同步优化背景为了满足项目需求,需要将大量数据的数据写入到ES进行检索,预估数据量是40亿左右,目前需要同步进去的是2亿左右。ES集群配置三台128G的国产服务器国产linux系统CPU主频低的拉跨JDK8的版本机械硬盘遇到的问题后端使用Java调用es的bulkapi......
  • Elasticsearch
    1,Elasticsearch简介1)分布式实时文件存储,可以将每一个字段都编入索引,使其可以被检索2)可以作为一个大型分布式集群(数百台服务器)技术,处理PB级数据3)Elasticsearch不是什么新技术,主要是将全文检索、数据分析以及分布式技术,合并在了一起,才形成了独一无二的ES2,基本概念1)Node(节点):Ela......
  • 【ElasticSearch】入门-ES的选主流程
    一、ES集群模式ES使用主从模式,因为ES的典型场景中的另一个简化是集群中没有那么多节点。通常节点数量远远小于单个节点能够维护的连接数,并且网络环境并不需要经常处理节点的加入和离开。1、选举算法ES中主要使用Bully算法作为选举算法(优点是易于实现)Bully算法:假定所有的节......
  • Elasticsearch不同集群间备份恢复(S3存储)
    S3存储首先都知道需要在ES集群上安装S3插件以及重启集群在MINIO集群创建相应的桶Kibana上注册快照存储库,两个不同的集群需要对接到同一个S3存储库,对接后会自动识别桶里的快照《见上一篇博客》恢复搭建的恢复集群已对接到备份集群对接的MINIO集群了可以看到已自动识别到......
  • 巧记Elasticsearch常用DSL语法
    记知识先记轮廓,关于DSL语法的轮廓,记住以下3句话即可:索引、文档和查询Match、Term和Bool还有翻页和聚合1、又爱又恨的DSL使用Elasticsearch时,我们一般是调用RestClientAPI的方式读取和写入集群数据。有时也会使用工具查阅和操作数据,比如:使用Chrome插件MultiElasticsearch......
  • Springboot项目中使用Elasticsearch的RestClient
    上一篇介绍了Elasticsearch的入门《5000字详说Elasticsearch入门(一)》,本篇介绍Springboot如何集成使用Elasticsearch。分为3步:配置properties文件、引入pom依赖、配置RestHighLevelClient类。1、选择ES的ClientAPI我们知道Elasticsearch是一款RestfulAPI风格的分布式搜索引擎......
  • Elasticsearch与文件描述符的恩恩怨怨
    提到Elasticsearch,让笔者最恶心的倒不是它的反人类的DSL设计,而是每次安装都需要修改进程的最大文件描述符。那ES与文件描述符有啥恩怨呢,下面就来唠叨唠叨。首先说说文件描述符、在说说ES为什么要这么多文件描述符。一、文件描述符1、什么是文件描述符文件描述符(Filedescriptor......
  • Elasticsearch-Alias别名的2个核心场景
    了解Elasticsearch的Alias别名之后,可以在业务上很方便的实现复杂需求,快速解决问题,本文从3个方面介绍:官方定义、使用场景、使用方法。一、官方定义先看下官方对ES的Alias定义:重点有2个:别名是一组索引的辅助名称,一个别名可以指向多个索引,一个索引可以有多个别名。使用别名后......