Before attempting this problem, you should be comfortable with:
Binary Search Tree Properties - Understanding that inorder traversal yields sorted values
Inorder Traversal - Traversing a BST to process nodes in ascending order
Binary Search - Finding insertion points or closest values in sorted arrays
Heaps/Priority Queues - Using max-heaps to maintain k closest elements efficiently
Two Pointers/Sliding Window - Expanding outward from a position to find k nearest values
1. Sort With Custom Comparator
Intuition
The simplest approach is to collect all node values from the tree and then sort them based on their distance to the target. Once sorted, the first k elements are the closest values. This works because we just need the k values closest to the target, regardless of their original position in the tree.
Algorithm
Perform a DFS traversal to collect all node values into an array.
Sort the array using a custom comparator that compares the absolute difference between each value and the target.
Return the first k elements from the sorted array.
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/publicclassSolution{publicIList<int>ClosestKValues(TreeNode root,double target,int k){List<int> arr =newList<int>();Dfs(root, arr);
arr.Sort((a, b)=> Math.Abs(a - target).CompareTo(Math.Abs(b - target)));return arr.Take(k).ToList();}privatevoidDfs(TreeNode node,List<int> arr){if(node ==null)return;
arr.Add(node.val);Dfs(node.left, arr);Dfs(node.right, arr);}}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/funcclosestKValues(root *TreeNode, target float64, k int)[]int{
arr :=[]int{}var dfs func(node *TreeNode)
dfs =func(node *TreeNode){if node ==nil{return}
arr =append(arr, node.Val)dfs(node.Left)dfs(node.Right)}dfs(root)
sort.Slice(arr,func(i, j int)bool{return math.Abs(float64(arr[i])-target)< math.Abs(float64(arr[j])-target)})return arr[:k]}
/**
* Definition for a binary tree node.
* class TreeNode(var `val`: Int = 0) {
* var left: TreeNode? = null
* var right: TreeNode? = null
* }
*/class Solution {funclosestKValues(root: TreeNode?, target: Double, k: Int): List<Int>{val arr = mutableListOf<Int>()fundfs(node: TreeNode?){if(node ==null)return
arr.add(node.`val`)dfs(node.left)dfs(node.right)}dfs(root)
arr.sortBy{ Math.abs(it - target)}return arr.take(k)}}
/**
* Definition for a binary tree node.
* public class TreeNode {
* public var val: Int
* public var left: TreeNode?
* public var right: TreeNode?
* public init() { self.val = 0; self.left = nil; self.right = nil; }
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
* self.val = val
* self.left = left
* self.right = right
* }
* }
*/classSolution{funcclosestKValues(_ root:TreeNode?,_ target:Double,_ k:Int)->[Int]{var arr =[Int]()funcdfs(_ node:TreeNode?){guardlet node = node else{return}
arr.append(node.val)dfs(node.left)dfs(node.right)}dfs(root)
arr.sort {abs(Double($0)- target)<abs(Double($1)- target)}returnArray(arr.prefix(k))}}
Time & Space Complexity
Time complexity: O(n⋅logn)
Space complexity: O(n)
Where n is the number of nodes in the tree
2. Traverse With Heap
Intuition
Instead of sorting all values, we can use a max-heap of size k to track the k closest values seen so far. As we traverse the tree, we compare each node's distance to the target with the farthest element in our heap. If the current node is closer, we replace the farthest element. This avoids sorting the entire array.
Algorithm
Create a max-heap that orders elements by their distance from the target (farthest at the top).
Traverse all nodes in the tree using DFS.
For each node, add its value to the heap. If the heap size exceeds k, remove the element with the largest distance.
Where n is the number of nodes in the tree and k is the size of our heap
3. Inorder Traversal + Sliding Window
Intuition
Since this is a BST, an inorder traversal gives us values in sorted order. With a sorted array, the k closest values to the target form a contiguous subarray. We can use binary search to find the position closest to the target, then expand outward using two pointers to collect the k nearest values.
Algorithm
Perform an inorder traversal to get all values in sorted order.
Use binary search to find the position where the target would be inserted.
Initialize two pointers: left pointing to the element just before the insertion point, right pointing at the insertion point.
Compare distances at both pointers and pick the closer one, moving that pointer outward. Repeat until k elements are collected.
classSolution:defclosestKValues(self, root: TreeNode, target:float, k:int)-> List[int]:defdfs(node, arr):ifnot node:return
dfs(node.left, arr)
arr.append(node.val)
dfs(node.right, arr)
arr =[]
dfs(root, arr)
left = bisect_left(arr, target)-1
right = left +1
ans =[]whilelen(ans)< k:if right ==len(arr)orabs(arr[left]- target)<=abs(arr[right]- target):
ans.append(arr[left])
left -=1else:
ans.append(arr[right])
right +=1return ans
classSolution{publicList<Integer>closestKValues(TreeNode root,double target,int k){List<Integer> arr =newArrayList<>();dfs(root, arr);int start =0;double minDiff =Double.MAX_VALUE;for(int i =0; i < arr.size(); i++){if(Math.abs(arr.get(i)- target)< minDiff){
minDiff =Math.abs(arr.get(i)- target);
start = i;}}int left = start;int right = start +1;while(right - left -1< k){if(left <0){
right +=1;continue;}if(right == arr.size()||Math.abs(arr.get(left)- target)<=Math.abs(arr.get(right)- target)){
left -=1;}else{
right +=1;}}return arr.subList(left +1, right);}publicvoiddfs(TreeNode node,List<Integer> arr){if(node ==null){return;}dfs(node.left, arr);
arr.add(node.val);dfs(node.right, arr);}}
classSolution{/**
* @param {TreeNode} root
* @param {number} target
* @param {number} k
* @return {number[]}
*/closestKValues(root, target, k){const arr =[];this.dfs(root, arr);let left =this.bisectLeft(arr, target)-1;let right = left +1;const ans =[];while(ans.length < k){if(left <0){
ans.push(arr[right++]);}elseif(right >= arr.length){
ans.push(arr[left--]);}elseif(Math.abs(arr[left]- target)<= Math.abs(arr[right]- target)){
ans.push(arr[left--]);}else{
ans.push(arr[right++]);}}return ans;}bisectLeft(arr, target){let lo =0, hi = arr.length;while(lo < hi){const mid = Math.floor((lo + hi)/2);if(arr[mid]< target) lo = mid +1;else hi = mid;}return lo;}dfs(node, arr){if(!node)return;this.dfs(node.left, arr);
arr.push(node.val);this.dfs(node.right, arr);}}
publicclassSolution{publicIList<int>ClosestKValues(TreeNode root,double target,int k){var arr =newList<int>();Dfs(root, arr);int left =BinarySearch(arr, target)-1;int right = left +1;var ans =newList<int>();while(ans.Count < k){if(left <0){
ans.Add(arr[right++]);}elseif(right >= arr.Count){
ans.Add(arr[left--]);}elseif(Math.Abs(arr[left]- target)<= Math.Abs(arr[right]- target)){
ans.Add(arr[left--]);}else{
ans.Add(arr[right++]);}}return ans;}privateintBinarySearch(List<int> arr,double target){int lo =0, hi = arr.Count;while(lo < hi){int mid =(lo + hi)/2;if(arr[mid]< target) lo = mid +1;else hi = mid;}return lo;}privatevoidDfs(TreeNode node,List<int> arr){if(node ==null)return;Dfs(node.left, arr);
arr.Add(node.val);Dfs(node.right, arr);}}
funcclosestKValues(root *TreeNode, target float64, k int)[]int{
arr :=[]int{}var dfs func(node *TreeNode)
dfs =func(node *TreeNode){if node ==nil{return}dfs(node.Left)
arr =append(arr, node.Val)dfs(node.Right)}dfs(root)
left := sort.Search(len(arr),func(i int)bool{returnfloat64(arr[i])>= target
})-1
right := left +1
ans :=[]int{}forlen(ans)< k {if left <0{
ans =append(ans, arr[right])
right++}elseif right >=len(arr){
ans =append(ans, arr[left])
left--}elseif math.Abs(float64(arr[left])-target)<= math.Abs(float64(arr[right])-target){
ans =append(ans, arr[left])
left--}else{
ans =append(ans, arr[right])
right++}}return ans
}
class Solution {funclosestKValues(root: TreeNode?, target: Double, k: Int): List<Int>{val arr = mutableListOf<Int>()fundfs(node: TreeNode?){if(node ==null)returndfs(node.left)
arr.add(node.`val`)dfs(node.right)}dfs(root)var left = arr.binarySearch(target.toInt()).let{if(it <0)-it -2else it -1}var right = left +1val ans = mutableListOf<Int>()while(ans.size < k){if(left <0){
ans.add(arr[right++])}elseif(right >= arr.size){
ans.add(arr[left--])}elseif(Math.abs(arr[left]- target)<= Math.abs(arr[right]- target)){
ans.add(arr[left--])}else{
ans.add(arr[right++])}}return ans
}}
classSolution{funcclosestKValues(_ root:TreeNode?,_ target:Double,_ k:Int)->[Int]{var arr =[Int]()funcdfs(_ node:TreeNode?){guardlet node = node else{return}dfs(node.left)
arr.append(node.val)dfs(node.right)}dfs(root)varleft=bisectLeft(arr, target)-1varright=left+1var ans =[Int]()while ans.count < k {ifleft<0{
ans.append(arr[right])right+=1}elseifright>= arr.count {
ans.append(arr[left])left-=1}elseifabs(Double(arr[left])- target)<=abs(Double(arr[right])- target){
ans.append(arr[left])left-=1}else{
ans.append(arr[right])right+=1}}return ans
}privatefuncbisectLeft(_ arr:[Int],_ target:Double)->Int{var lo =0, hi = arr.count
while lo < hi {let mid =(lo + hi)/2ifDouble(arr[mid])< target {
lo = mid +1}else{
hi = mid
}}return lo
}}
Time & Space Complexity
Time complexity: O(n+k)
Space complexity: O(n)
Where n is the number of nodes in the tree and k is the size of our sliding window
4. Binary Search The Left Bound
Intuition
Since the inorder traversal produces a sorted array and we need a contiguous subarray of size k, we can binary search for the optimal starting position of this window. For any starting position, we compare the distances of the leftmost and rightmost elements in the window to decide if shifting right would improve our answer.
Algorithm
Perform an inorder traversal to get all values in sorted order.
Binary search for the left boundary of the k-element window. The search range is from index 0 to n-k.
At each midpoint, compare the distance of the element at mid with the element at mid+k. If the element at mid+k is closer, shift the window right; otherwise, keep searching left.
Return the subarray starting at the found left boundary with length k.
Where n is the number of nodes in the tree and k is the number of closest values to return
5. Build The Window With Deque
Intuition
During inorder traversal, values are visited in sorted order. We can maintain a sliding window of size k using a deque. As we visit each node, we add it to the window. When the window exceeds k elements, we compare the distances of the first and last elements and remove the one farther from the target. Once the first element is closer, all subsequent elements will be even farther, so we can stop early.
Algorithm
Perform an inorder traversal of the tree.
Maintain a deque to store the current window of candidates.
For each visited node, append its value to the deque.
If the deque size exceeds k, compare the front and back elements. If the front is closer or equal, remove the back and stop traversing the right subtree. Otherwise, remove the front and continue.
Where n is the number of nodes in the tree and k is the number of closest values to return
Common Pitfalls
Not Exploiting BST Property
Using a generic tree traversal and sorting all values works but misses the O(n + k) optimization possible with BST. Inorder traversal gives sorted values, enabling efficient sliding window approaches.
Off-by-One Errors in Binary Search
When using binary search to find the starting position for the sliding window, the insertion point may be at index 0 or n, causing out-of-bounds access when initializing the left pointer.
# Wrong: Can cause index -1 access
left = bisect_left(arr, target)-1# If target < all elements, left = -1# Must check: if left < 0 before accessing arr[left]
Using Wrong Heap Type
When using a heap to track k closest values, a max-heap is needed to evict the farthest element. Using a min-heap evicts the closest element instead, producing incorrect results.
When two values have equal distance to target, the problem may require returning the smaller value. Failing to handle ties correctly can produce wrong results.
Stopping Early Without Checking Both Sides
In the sliding window approach, stopping as soon as one pointer goes out of bounds forgets to collect remaining elements from the other side.