Closest Values In BST Problem

Closest Values In A BST Problem Statement

Given a non-empty binary search tree (BST) and a target value, find k values in the BST that are the closest to the target.

Example

example1

"target": 2
"k": 2

Output:

[1, 2]

As we can see the target value is 2 and [1, 2] are the nearest values (in terms of the absolute difference between target value and node values). Besides this, the answer [2, 3] is also correct, because abs(2 – 1) = abs(2 – 3). You can return any of them. You can also return your answer in any order, that is [1, 2] or [2, 1], both are correct.

Notes

  • The function accepts the root of the BST.
  • Return the k nearest values to the given target value.
  • The values in the BST are NOT necessarily distinct.
  • If there are multiple correct answers, return any one.

Constraints:

  • 1 <= number of nodes in the tree <= 100000
  • 1 <= values stored in the nodes <= 109
  • 0 <= target value <= 109
  • 1 <= k <= n
  • number of edges = n - 1

We have provided one solution. We will refer to the number of nodes in the given binary tree by n and number of edges by m.

  • We have to find k points in the BST that are the closest to the given target value.
  • We will start an in-order traversal of the given input tree.
  • Besides this, we will keep a priority queue of size k to store the closest points.
  • So, while traversing we keep on inserting the values in the Priority Queue until the size reaches k.
  • Once the size reaches k, we check whether the current value is closer to the target value than the first value (that is the head) of the priority queue.
  • If the current value is closer, we remove the head and push this value. Otherwise, we stop traversing as the further values would certainly not be closer as well because all the values after the current values will be larger than the current one.
  • After the complete in-order traversal, the values remaining in the Priority Queue are the required closest points.

Time Complexity

O(n * log(k)).

We are traversing the entire input tree and at the same time inserting nodes in the priority queue if they have a possibility to be part of the final answer. So, the time needed for this is O(n * log(k)).

Auxiliary Space Used

O(n + k).

We create a Priority Queue of size k to store the closest points to the target value. Besides this, we perform an inorder tree traversal which takes O(n) stack space due to recursion. So, the total auxiliary space used is O(n + k).

Space Complexity

O(n + m + k).

Space taken by input: O(n + m).

Auxiliary space used: O(n + k).

Space used by output: O(k).

Hence, total space complexity will be O(n + m + k).

We hope that these solutions to the Closest values in a BST problem have helped you level up your coding skills. You can expect problems like these at top tech companies like Amazon and Google.

If you are preparing for a tech interview at FAANG or any other Tier-1 tech company, register for Interview Kickstart’s FREE webinar to understand the best way to prepare.

Interview Kickstart offers interview preparation courses taught by FAANG+ tech leads and seasoned hiring managers. Our programs include a comprehensive curriculum, unmatched teaching methods, and career coaching to help you nail your next tech interview.

We offer 18 interview preparation courses, each tailored to a specific engineering domain or role, including the most in-demand and highest-paying domains and roles, such as:

‍To learn more, register for the FREE webinar.

Try yourself in the Editor

Note: Input and Output will already be taken care of.

Add Two Numbers Represented By Lists Problem

Binary Tree Level Order Traversal Problem with Examples

Jump Game

Zigzag Sort Problem

Boggle Solver Problem

Shortest String Transformation Using A Dictionary Problem

Register for our webinar

How to Nail your next Technical Interview

Loading_icon
Loading...
1 Enter details
2 Select slot
By sharing your contact details, you agree to our privacy policy.

Select a Date

Time slots

Time Zone:

Get tech interview-ready to navigate a tough job market

Best suitable for: Software Professionals with 5+ years of exprerience
Register for our FREE Webinar

Next webinar starts in

00
DAYS
:
00
HR
:
00
MINS
:
00
SEC