You're given the root of a binary search tree, a target value, and an integer k. Your task is to find the k values in the BST that are closest to the target.

Think of it as finding your k nearest neighbors in a sorted tree structure! The beauty of this problem lies in leveraging the BST's sorted property to efficiently locate the closest values without examining every single node.

Key Points:

  • You may return the answer in any order
  • You're guaranteed to have exactly one unique set of k closest values
  • "Closest" means smallest absolute difference: |node.val - target|

Example: If target=3.714286 and k=2 in BST [4,2,5,1,3], the two closest values are [4,3] since |4-3.714286|=0.285714 and |3-3.714286|=0.714286 are the smallest differences.

Input & Output

example_1.py — Basic Case
$ Input: root = [4,2,5,1,3], target = 3.714286, k = 2
Output: [4,3]
💡 Note: The node values in the BST are [1,2,3,4,5]. The distances to target 3.714286 are: |1-3.714286|=2.714286, |2-3.714286|=1.714286, |3-3.714286|=0.714286, |4-3.714286|=0.285714, |5-3.714286|=1.285714. The 2 closest values are 4 and 3.
example_2.py — Single Node
$ Input: root = [1], target = 0.000000, k = 1
Output: [1]
💡 Note: There's only one node in the BST, so we return its value. Distance is |1-0|=1.
example_3.py — All Nodes Required
$ Input: root = [3,1,4,null,2], target = 2.5, k = 4
Output: [2,3,1,4]
💡 Note: All nodes in the BST are [1,2,3,4]. Distances to target 2.5: |1-2.5|=1.5, |2-2.5|=0.5, |3-2.5|=0.5, |4-2.5|=1.5. Since k=4, we return all nodes. Note that 2 and 3 are equidistant, so either ordering is acceptable.

Constraints

  • The number of nodes in the tree is n where 1 ≤ n ≤ 104
  • 0 ≤ Node.val ≤ 109
  • -109 ≤ target ≤ 109
  • 1 ≤ k ≤ n
  • It's guaranteed that there is only one unique set of k values in the BST that are closest to the target

Visualization

Tap to expand
Closest Binary Search Tree Value II INPUT Binary Search Tree 4 2 5 1 3 root = [4,2,5,1,3] target = 3.714286 k = 2 ALGORITHM STEPS 1 Inorder Traversal Get sorted values: [1,2,3,4,5] 2 Two Pointers Find closest element index 3 Compare Distances Expand window left/right 4 Collect k Values Return k closest elements Distance Calculations |1 - 3.71| = 2.71 |2 - 3.71| = 1.71 |3 - 3.71| = 0.71 [OK] |4 - 3.71| = 0.29 [OK] |5 - 3.71| = 1.29 FINAL RESULT Closest Values Highlighted 4 2 5 1 3 Output: [4, 3] k=2 closest values to 3.714 4 (dist: 0.29) 3 (dist: 0.71) OK - Smallest distances! Key Insight: BST's inorder traversal gives sorted values. Use two pointers starting from the closest element, expanding left or right based on which neighbor is closer to target. This achieves O(n) time complexity compared to O(n log n) sorting approach. The BST property enables efficient binary search for starting point. TutorialsPoint - Closest Binary Search Tree Value II | Optimal Solution
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28 Apple 22
67.9K Views
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