java.lang.Object
g2401_2500.s2476_closest_nodes_queries_in_a_binary_search_tree.Solution

public class Solution extends Object
2476 - Closest Nodes Queries in a Binary Search Tree.<p>Medium</p> <p>You are given the <code>root</code> of a <strong>binary search tree</strong> and an array <code>queries</code> of size <code>n</code> consisting of positive integers.</p> <p>Find a <strong>2D</strong> array <code>answer</code> of size <code>n</code> where <code>answer[i] = [min<sub>i</sub>, max<sub>i</sub>]</code>:</p> <ul> <li><code>min<sub>i</sub></code> is the <strong>largest</strong> value in the tree that is smaller than or equal to <code>queries[i]</code>. If a such value does not exist, add <code>-1</code> instead.</li> <li><code>max<sub>i</sub></code> is the <strong>smallest</strong> value in the tree that is greater than or equal to <code>queries[i]</code>. If a such value does not exist, add <code>-1</code> instead.</li> </ul> <p>Return <em>the array</em> <code>answer</code>.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2022/09/28/bstreeedrawioo.png" alt="" /></p> <p><strong>Input:</strong> root = [6,2,13,1,4,9,15,null,null,null,null,null,null,14], queries = [2,5,16]</p> <p><strong>Output:</strong> [[2,2],[4,6],[15,-1]]</p> <p><strong>Explanation:</strong> We answer the queries in the following way:</p> <ul> <li>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].</li> <li>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].</li> <li>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].</li> </ul> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2022/09/28/bstttreee.png" alt="" /></p> <p><strong>Input:</strong> root = [4,null,9], queries = [3]</p> <p><strong>Output:</strong> <a href="-1,4">-1,4</a></p> <p><strong>Explanation:</strong> 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].</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li> <li><code>1 <= Node.val <= 10<sup>6</sup></code></li> <li><code>n == queries.length</code></li> <li><code>1 <= n <= 10<sup>5</sup></code></li> <li><code>1 <= queries[i] <= 10<sup>6</sup></code></li> </ul>