Skip to content

Practice Closest Nodes Queries in a Binary Search Tree

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2476
LeetCode Problem

Closest Nodes Queries in a Binary Search Tree

Problem Statement

Given the root of a binary search tree and queries, for each query value, return [floor,ceiling] from the values in root: floor is the largest tree value no greater than the query, and ceiling is the smallest no less than it. Use -1 separately for either missing bound. Return pairs in query order.

Source: Closest Nodes Queries in a Binary Search Tree on LeetCode. Use the original task for its coding environment and submissions.

Examples

# 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]]

# Example 2

Input
root = [4,null,9], queries = [3]
Output
[[-1,4]]

Constraints

The number of nodes in the tree is in the range [2, 10^5].
1 <= Node.val <= 10^6
n == queries.length
1 <= n <= 10^5
1 <= queries[i] <= 10^6

Related LeetCode Problems

Practice more problems similar to Closest Nodes Queries in a Binary Search Tree.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.