AlgoMaster Logo

Closest Binary Search Tree Value

Last Updated: March 29, 2026

easy

Understanding the Problem

We have a binary search tree and a target value (which can be a decimal), and we need to find which node value is closest to that target. If two node values are equally close, we return the smaller one.

The BST property is the key here. In a BST, every node's left subtree contains only values smaller than the node, and every node's right subtree contains only values larger. This ordering means we don't need to check every single node. If the target is less than the current node, the answer can't be hiding in the right subtree (well, it could be the current node, but nothing further right will be closer). So we can prune our search, much like binary search on a sorted array.

Key Constraints:

  • 1 <= number of nodes <= 10^4 → With up to 10,000 nodes, even an O(n) traversal is acceptable. But we can do better by leveraging the BST structure.
  • 0 <= Node.val <= 10^9 → Node values are non-negative and can be large. No overflow concerns in Java/C++ since we'll use long or double for distance calculations.
  • -10^9 <= target <= 10^9 → Target can be negative even though node values are non-negative. Also, the target is a floating-point number, so the closest node value might not be an exact match.

Approach 1: Inorder Traversal (Brute Force)

Intuition

The most straightforward idea is to collect all the values in the BST and then find which one is closest to the target. Since an inorder traversal of a BST gives us values in sorted order, we can traverse the entire tree, store every value, and then scan through the list to find the minimum absolute difference.

This ignores the BST structure for the search itself, essentially treating it as "give me all the values and I'll figure it out." It works, but it's wasteful because we're visiting every node when we don't have to.

Algorithm

  1. Perform an inorder traversal of the BST to collect all node values into a list.
  2. Iterate through the list and track the value with the smallest absolute difference from the target.
  3. If two values have the same difference, keep the smaller one.
  4. Return the closest value.

Example Walkthrough

1Inorder traversal: visit left subtree first. Go to node 2
42visit135
1/6

Code

We're visiting every single node even though the BST property tells us which direction to search. What if we walked down the tree like a binary search, only visiting one child at each level?

Approach 2: BST-Guided Search (Iterative)

Intuition

Here's the key insight: in a BST, we can navigate toward the target just like binary search navigates through a sorted array. At each node, we compare the target to the node's value. If the target is smaller, we go left. If it's larger, we go right. Along the way, we keep track of the closest value we've seen.

The beauty of this approach is that we never need to backtrack. At each node, the BST property guarantees that the subtree we skip can't contain a closer value than what we've already found on our path.

Algorithm

  1. Initialize closest to the root's value.
  2. Start at the root and walk down the tree.
  3. At each node, compare its value to the current closest. Update closest if the current node is nearer to the target (or equally near but smaller).
  4. If target < node.val, move to the left child. Otherwise, move to the right child.
  5. When we reach a null node, return closest.

Example Walkthrough

1Start at root. node=4, |4-3.5|=0.5. closest=4
4closest=42135
1/5

Code

This is the same algorithm expressed recursively, which some find more natural for tree problems.

Approach 3: Recursive BST Search

Intuition

This is the same algorithm as Approach 2, but expressed recursively. Some people find recursive tree code more natural to write, especially in interviews where you're already thinking about tree problems recursively.

The idea is identical: at each node, update the closest value, then recurse into the appropriate subtree. The recursion naturally stops when we hit a null node.

Algorithm

  1. Base case: if the node is null, return the current closest value.
  2. Compare the current node's value with closest and update if it's nearer (or equally near but smaller).
  3. If target < node.val, recurse left. Otherwise, recurse right.
  4. Return the result from the recursive call.

Example Walkthrough

1dfs(4, closest=4). |4-3.714|=0.286. closest=4
4closest=42135
1/5

Code