Grokking LinkedIn Coding Interview
Ask Author
Back to course home

0% completed

Solution: Closest Binary Search Tree Value II

Problem Statement

Given the root of a binary search tree, an integer target, and an integer k, return a 1D array containing k values from the BST which are closest to the given target value. You may return the answer in any order.

Examples

  • Example 1:
    • Input: BST = [10,5,15,2,7,null,18], target = 8.5, k = 3
  • Expected Output: [7,10,5]

  • Justification: The three numbers closest to 8.5 in the BST are 7, 10, and 5.

  • Example 2:

    • Input: BST = [20,10,30,5,15,null,null,3,8,null,17], target = 15.5, k = 2
  • Expected Output: [15,17]

.....

.....

.....

Like the course? Get enrolled and start learning!