Search⌘ K
AI Features

Solution: Closest Binary Search Tree Value

Explore how to locate the closest node value to a given target in a binary search tree by leveraging BST traversal. Understand how to compare distances, break ties by choosing the smaller value, and optimize the search by pruning branches, ensuring efficient performance with minimal space usage.

Statement

Given the root of a binary search tree and a floating point target value, find the node value in the BST that is closest to target. If there are multiple node values with the same minimum distance to target, return the smallest among them.

Note: The target is a floating point number, while all node values are integers.

Constraints:

  • The number of nodes in the tree is in the range [1,104][1, 10^4] ...