Back to problems

Closest Binary Search Tree Value

Algorithm · Roblox · Medium

Given a BST root node and a numeric target, find the tree value whose distance from target is minimal. When two or more values are equally close, choose the lowest value among them. root is a TreeNode representing the tree. A TreeNode has a numeric val and left and right child references, which may be null. In the examples, the tree is shown as a level-order list of node values. A binary search tree follows these properties: Every key in a node's left subtree is smaller than…

Checking your access…