Back to problems

Find closest value to a target in a BST

Algorithm · DoorDash · Medium

Given a binary search tree encoded as a level-order array and a real number target, return the BST node value whose distance from target is smallest. If two node values have the same distance from target, return the smaller value. The array tree describes the BST in breadth-first order: tree[0] is the root, and for every non-null node its left child and then its right child occupy the next two positions. A missing child is represented by null, so later positions remain…

Checking your access…