


A Binary Search Tree is a binary tree data structure with the following properties:
The left subtree of any node contains nodes with a value less than the node’s value.
The right subtree of any node contains nodes with a value equal to or greater than the node’s value.
Right, and left subtrees are also binary search trees.
It is guaranteed that,
Values of all nodes in the given binary search tree are distinct positive integers.
There will be only one unique set of ‘K’ values in the binary search tree that is closest to the ‘target’.
The first line of the input contains an integer ‘T’ representing the number of test cases.
The first line of each test case contains a single real number, ‘target’ denoting the target value given in the problem.
The second line of each test case contains a single integer ‘K’, denoting the number of values that are to be selected from the binary search tree.
The third line of each test case contains elements of the tree in the level order form. The line consists of values of nodes separated by a single space. In case a node is null, we take -1 in its place.
For example, the input for the tree is depicted in the below image.

25
20 30
15 23 28 35
-1 -1 -1 -1 -1 -1 -1 -1
Level 1 :
The root node of the tree is 25
Level 2 :
Left child of 25 = 20
Right child of 25 = 30
Level 3 :
Left child of 20 = 15
Right child of 20 = 23
Left child of 30 = 28
Right child of 30 = 35
Level 4 :
Left child of 15 = null (-1)
Right child of 15 = null (-1)
Left child of 23 = null (-1)
Right child of 23 = null (-1)
Left child of 28 = null (-1)
Right child of 28 = null (-1)
Left child of 35 = null (-1)
Right child of 35 = null (-1)
The first not-null node(of the previous level) is treated as the parent of the first two nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.
The input ends when all nodes at the last level are null(-1).
The above format was just to provide clarity on how the input is formed for a given tree.
The sequence will be put together in a single line separated by a single space. Hence, for the above-depicted tree, the input will be given as:
25 20 30 15 23 28 35 -1 -1 -1 -1 -1 -1 -1 -1
For each test case, print ‘K’ space-separated integer values from the given binary search tree that is closest to the ‘target’ in sorted order.
The output of each test case will be printed in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N <= 100
1 <= data <= 10 ^ 9
0 <= K <= N
-10 ^ 9 <= target <= 10 ^ 9
Time Limit: 1 sec
The idea here is to traverse the given binary search tree, and from all the available values in the tree, find the value which is closest (having least absolute difference) to ‘target’ and mark this value as taken. Repeat this process for ‘K’ times.
This function is used to find a value from the available values in the binary search tree that is closest to the ‘target’
This function will take four parameters :
void findClosest(root, target, minDifference, value):
This function is used to find a value from the given binary search tree that is the same as ‘value’ and mark it as taken.
This function will take two parameters :
void mark(root, value):
The idea here is to traverse the given binary search tree and insert all the values present in the tree into an array of integers. After inserting the values, we can sort the elements of this array in increasing order of absolute difference from ‘target’. After sorting, the first ‘K’ elements of this array will be the ‘K’ closest values from ‘target’.
This function is used to traverse the given binary search tree and insert all the values present in BST into ‘arrValues’
This function will take two parameters :
void traverse(root, arrValues):
The idea here is to traverse the given binary search tree and use a max - heap data structure to keep track of the ‘K’ closest values from ‘target’. We will traverse the given binary search tree, and for every value present in the tree, we will insert the absolute difference of the current value from ‘target’ into the max - heap. If the number of elements present in the max heap becomes greater than ‘K’, we will remove the element from the max - heap that has the maximum absolute difference from ‘target’.
This function is used to traverse the given binary search tree and store ‘K’ closest values from ‘target’ into ‘maxHeap’
This function will take four parameters :
void traverse(root, target, maxHeap, k):
The idea here is to do an Inorder traversal of the given binary search tree. In inorder traversal of the binary search tree, we traverse all the values in increasing order. We will use a Double-ended queue to keep track of the ‘K’ closest values from ‘target’. We will traverse the given binary search tree, and for every value present in the tree, we will insert the absolute difference of the current value from ‘target’ into the double-ended queue. If the number of elements present in the double-ended queue becomes greater than ‘K’, we will compare the first and the last absolute difference values in the queue and remove the greater value from the queue.
This function is used to do an inorder traversal of the given binary search tree and store ‘K’ closest values from ‘target’ into ‘dQueue’
This function will take four parameters :
void inorderTraversal(root, target, dQueue, k):