Watch 10 video solutions for Closest Nodes Queries in a Binary Search Tree, a medium level problem involving Array, Binary Search, Tree. This walkthrough by Hua Hua has 1,906 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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: - 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:
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] <= 106Problem Overview: Given the root of a binary search tree and multiple query values, return two numbers for each query: the largest value in the tree less than or equal to the query (floor) and the smallest value greater than or equal to it (ceiling). If either value doesn't exist, return -1. The BST property makes it possible to search efficiently.
Approach 1: In-order Traversal and Binary Search (Time: O(n + q log n), Space: O(n))
An in-order traversal of a BST produces a sorted array of node values. Traverse the tree once using depth-first search and store the values in a list. For each query, run binary search on the sorted list to locate the insertion position. The value at or before that index gives the floor, and the value at or after it gives the ceiling. This approach is straightforward and efficient when there are many queries because the tree is processed only once.
Approach 2: Recursive Query Processing without Additional Inorder List (Time: O(q * h), Space: O(h))
Instead of building a sorted list, process each query directly on the BST. Start from the root and move left or right depending on how the query compares to the current node value. Track two variables during traversal: the best candidate for floor and the best candidate for ceiling. Moving left updates the ceiling candidate; moving right updates the floor candidate. Because each query follows a single root-to-leaf path, the time per query is proportional to the tree height h. This avoids storing an extra array but may be slower if the tree is skewed.
Recommended for interviews: The in-order traversal with binary search is the most commonly expected solution. It clearly uses the BST property and reduces repeated work when handling many queries. Implementing the direct BST traversal approach shows deeper understanding of tree navigation and space optimization, but interviewers typically expect the sorted-array plus binary-search pattern first.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| In-order Traversal + Binary Search | O(n + q log n) | O(n) | Best when there are many queries and you want fast lookups after preprocessing |
| Direct BST Traversal per Query | O(q * h) (O(q log n) average, O(qn) worst) | O(h) | When you want to avoid extra memory or when the tree height is small |