Closest Nodes Queries in a Binary Search Tree - Problem
You are given the root of a binary search tree and an array queries of positive integers. For each query value, you need to find two important pieces of information:

1. Floor value: The largest value in the BST that is smaller than or equal to the query
2. Ceiling value: The smallest value in the BST that is greater than or equal to the query

Return a 2D array where each element answer[i] = [mini, maxi] represents the floor and ceiling for queries[i]. If no floor exists, use -1. If no ceiling exists, use -1.

Think of it as finding the closest neighbors for each query in the BST!

Input & Output

example_1.py — Basic BST Query
$ 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]]
💡 Note: For query 2: floor=2 (exact match), ceiling=2 (exact match). For query 5: floor=4 (largest ≤5), ceiling=6 (smallest ≥5). For query 16: floor=15 (largest ≤16), ceiling=-1 (no value ≥16).
example_2.py — Edge Case Queries
$ Input: root = [4,null,9], queries = [3,10]
Output: [[-1,4],[9,-1]]
💡 Note: For query 3: no value ≤3 exists (floor=-1), smallest value ≥3 is 4 (ceiling=4). For query 10: largest value ≤10 is 9 (floor=9), no value ≥10 exists (ceiling=-1).
example_3.py — Single Node
$ Input: root = [5], queries = [1,5,10]
Output: [[-1,5],[5,5],[5,-1]]
💡 Note: Tree has only one node (5). Query 1: floor=-1, ceiling=5. Query 5: floor=5, ceiling=5. Query 10: floor=5, ceiling=-1.

Constraints

  • The number of nodes in the tree is in the range [1, 2 × 104]
  • 1 ≤ Node.val ≤ 106
  • n == queries.length
  • 1 ≤ n ≤ 104
  • 1 ≤ queries[i] ≤ 106
  • All node values are unique

Visualization

Tap to expand
Closest Nodes Queries in BST INPUT Binary Search Tree 6 2 13 1 4 9 15 14 In-order: [1,2,4,6,9,13,14,15] Queries: 2 5 16 Find floor and ceiling ALGORITHM STEPS 1 Convert BST to Sorted Array In-order traversal: O(n) 2 Binary Search for Floor Find largest val <= query 3 Binary Search for Ceiling Find smallest val >= query 4 Return [floor, ceiling] Use -1 if not found Query Processing: q=2: arr=[1,2,4,6,9,13,14,15] floor=2, ceil=2 --> [2,2] q=5: between 4 and 6 floor=4, ceil=6 --> [4,6] q=16: > all values floor=15, ceil=-1 --> [15,16] Wait: 16>15, no ceil --> [-1,-1] FINAL RESULT Output Array: Query: 2 [2, 2] 2 exists in BST Query: 5 [4, 6] Between 4 & 6 Query: 16 [15, -1] No ceiling Final Answer: [[2,2],[4,6],[15,-1]] Time: O(n + q*log n) Space: O(n) n=nodes, q=queries Key Insight: Converting BST to sorted array via in-order traversal enables efficient binary search. For each query, use lower_bound/upper_bound to find floor (largest <= q) and ceiling (smallest >= q). TutorialsPoint - Closest Nodes Queries in a Binary Search Tree | Optimal Solution
Asked in
Microsoft 45 Amazon 38 Google 32 Meta 25
52.4K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen