Given a BST with N nodes and a target node K. The task is to find an integer having minimum absolute difference with given target value K and return this difference.
The first line of input contains the number of test cases T. For each test case, there will be only a single line of input which is a string representing the tree as described below:
The values in the string are in the order of level order traversal of the tree where, numbers denote node values, and a character “N” denotes NULL child.
For the above tree, the string will be: 1 2 3 N N 4 6 N 5 N N 7 N
Second-line is an integer represents K
The output for each test case will be the minimum absolute difference of a node with a given target value K.
This is a function problem. You don't have to take any input. Just complete the function maxDiff() that takes node and K as a parameter and returns the minimum difference.
1 <= T <= 10
1 <= N <= 100000
10 2 11 1 5 N N N N 3 6 N 4
8 1 9 N 4 N 10 3
K=13. The node that has value nearest to K is 11. so the answer is 2
K=9. The node that has value nearest to K is 9. so the answer is 0.
If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.