Find the Closest Element in BST
PROBLEM :
Given a binary search tree and a target node K. The task is to complete the function which returns an integer denoting the node having minimum absolute difference with given target value K.
Example
For above binary search tree
Input : k = 4
Output : 4
Input : k = 18
Output : 17
Input : k = 12
Output : 9
Input:
The first line of the input contains an integer 'T' denoting the number of test cases. Then 'T' test cases follow. Each test case consists of three lines. First line of each test case contains an integer N denoting the no of nodes of the BST . Second line of each test case consists of 'N' space separated integers denoting the elements of the BST. These elements are inserted into BST in the given order.The last line of each test case contains an integer k as specified in problem statement.
Output:
The output for each test case will be the value of the node with minimum absolute difference with given target value K.
Constraints:
1<=T<=100
1<=N<=200
Example(To be used only for expected output):
Input:
2
9
9 4 3 6 5 7 17 22 20
18
9
9 4 3 6 5 7 17 22 20
4
Output:
17
4
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*The Node structure is
struct Node {
int data;
Node * right, * left;
};*/
/*You are required to complete below method */
void MAXdiff(Node *,int ,int* ,int *) ;
int maxDiff(Node *root, int k)
{
int min,ele ;
min=INT_MAX ;
ele=0 ;
MAXdiff(root,k,&min,&ele) ;
return ele ;
}
void MAXdiff(Node *root,int k,int *min,int *ele)
{
if(root==NULL)
return ;
if((*min)>abs(k-root->data))
{
(*min)=abs(k-root->data) ;
(*ele)=root->data ;
}
MAXdiff(root->left,k,&(*min),&(*ele)) ;
MAXdiff(root->right,k,&(*min),&(*ele)) ;
}
---------------------------------------------------------------------------------
Given a binary search tree and a target node K. The task is to complete the function which returns an integer denoting the node having minimum absolute difference with given target value K.
Example
For above binary search tree
Input : k = 4
Output : 4
Input : k = 18
Output : 17
Input : k = 12
Output : 9
Input:
The first line of the input contains an integer 'T' denoting the number of test cases. Then 'T' test cases follow. Each test case consists of three lines. First line of each test case contains an integer N denoting the no of nodes of the BST . Second line of each test case consists of 'N' space separated integers denoting the elements of the BST. These elements are inserted into BST in the given order.The last line of each test case contains an integer k as specified in problem statement.
Output:
The output for each test case will be the value of the node with minimum absolute difference with given target value K.
Constraints:
1<=T<=100
1<=N<=200
Example(To be used only for expected output):
Input:
2
9
9 4 3 6 5 7 17 22 20
18
9
9 4 3 6 5 7 17 22 20
4
Output:
17
4
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*The Node structure is
struct Node {
int data;
Node * right, * left;
};*/
/*You are required to complete below method */
void MAXdiff(Node *,int ,int* ,int *) ;
int maxDiff(Node *root, int k)
{
int min,ele ;
min=INT_MAX ;
ele=0 ;
MAXdiff(root,k,&min,&ele) ;
return ele ;
}
void MAXdiff(Node *root,int k,int *min,int *ele)
{
if(root==NULL)
return ;
if((*min)>abs(k-root->data))
{
(*min)=abs(k-root->data) ;
(*ele)=root->data ;
}
MAXdiff(root->left,k,&(*min),&(*ele)) ;
MAXdiff(root->right,k,&(*min),&(*ele)) ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment