Search⌘ K
AI Features

Solution: Closest Binary Search Tree Value

Explore solving the closest value search in a binary search tree by leveraging depth-first search. Learn to traverse efficiently using BST properties to find the nearest node to a floating-point target. Understand how to update candidates and decide traversal direction while ensuring optimal time and space complexity.

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].

  • 00 \leq Node.val 10 ...