The tests write a tree as a level-order list (row by row, left to right) with None for a missing child: [3, 9, 20, None, None, 15, 7] is a root 3 with children 9 and 20, where 20 has children 15 and 7. All trees in this topic that are called BSTs have different values.
Return the value in the tree that is closest to target. If two values are equally close, return the smaller one.
For [4, 2, 5, 1, 3] and target = 3 the answer is 3; for [4, 2, 5, 1, 3] and target = 6 it is 5.
Example 1
Input:
closest_value(root, 3) # root = [4, 2, 5, 1, 3]
Output:
3
Example 2
Input:
closest_value(root, 6) # root = [4, 2, 5, 1, 3]
Output:
5
1 <= number of nodes <= 100000-10**9 <= target <= 10**9No results yet
Run your code to check it against the examples, or submit to run every test.