public class Solution extends Object
2476 - Closest Nodes Queries in a Binary Search Tree.
Medium
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:

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:
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:
[2, 105].1 <= Node.val <= 106n == queries.length1 <= n <= 1051 <= queries[i] <= 106| Constructor and Description |
|---|
Solution() |
| Modifier and Type | Method and Description |
|---|---|
List<List<Integer>> |
closestNodes(TreeNode root,
List<Integer> queries) |
Copyright © 2023. All rights reserved.