Print Common Nodes in BST
PROBLEM :
Given two Binary Search Trees, task is to complete the function printCommon, which print's the common nodes in them. In other words, find intersection of two BSTs.
Example
root1
5
/ \
1 10
/ \ /
0 4 7
\
9
root2:
10
/ \
7 20
/ \
4 9
Output: 4 7 9 10
Input:
The first line of the input contains an integer T, denoting the number of testcases. Then T test cases follow. For each test case two root nodes "root1" and "root2" pointing to the two tree's, are send to the function printCommon.
Output:
For each test case output a new line, containing space separated integers denoting the intersection of two BST's in sorted order.
Constraints:
1<=T<=50
1<=N<=50
Example:
Input:
1
7
5 1 10 0 4 7 9
5
10 7 20 4 9
Output:
4 7 9 10
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*Node structure
struct Node
{
int data;
struct Node *left, *right;
};*/
/* Function should print common elements in given two trees */
int sizeTree(Node *) ;
void Inorder(Node *,int [],int *) ;
void printCommon(Node *root1, Node *root2)
{
if(!root1||!root2)
return ;
int n=sizeTree(root1) ;
int m=sizeTree(root2) ;
int arr1[n] ,arr2[m] ;
int curr=0 ;
Inorder(root1,arr1,&curr) ;
curr=0 ;
Inorder(root2,arr2,&curr) ;
int i,j ;
i=0,j=0 ;
while(i!=n&&j!=m)
{
if(arr1[i]<arr2[j])
i++ ;
else if(arr1[i]>arr2[j])
j++ ;
else
{
cout<<arr1[i]<<" " ;
i++ ;
j++ ;
}
}
cout<<endl ;
}
int sizeTree(Node *root)
{
if(!root)
return 0 ;
return 1+sizeTree(root->left)+sizeTree(root->right) ;
}
void Inorder(Node *root,int arr[],int *curr)
{
if(root)
{
Inorder(root->left,arr,&(*curr)) ;
arr[(*curr)++]=root->data ;
Inorder(root->right,arr,&(*curr)) ;
}
}
--------------------------------------------------------------------------------
Given two Binary Search Trees, task is to complete the function printCommon, which print's the common nodes in them. In other words, find intersection of two BSTs.
Example
root1
5
/ \
1 10
/ \ /
0 4 7
\
9
root2:
10
/ \
7 20
/ \
4 9
Output: 4 7 9 10
Input:
The first line of the input contains an integer T, denoting the number of testcases. Then T test cases follow. For each test case two root nodes "root1" and "root2" pointing to the two tree's, are send to the function printCommon.
Output:
For each test case output a new line, containing space separated integers denoting the intersection of two BST's in sorted order.
Constraints:
1<=T<=50
1<=N<=50
Example:
Input:
1
7
5 1 10 0 4 7 9
5
10 7 20 4 9
Output:
4 7 9 10
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*Node structure
struct Node
{
int data;
struct Node *left, *right;
};*/
/* Function should print common elements in given two trees */
int sizeTree(Node *) ;
void Inorder(Node *,int [],int *) ;
void printCommon(Node *root1, Node *root2)
{
if(!root1||!root2)
return ;
int n=sizeTree(root1) ;
int m=sizeTree(root2) ;
int arr1[n] ,arr2[m] ;
int curr=0 ;
Inorder(root1,arr1,&curr) ;
curr=0 ;
Inorder(root2,arr2,&curr) ;
int i,j ;
i=0,j=0 ;
while(i!=n&&j!=m)
{
if(arr1[i]<arr2[j])
i++ ;
else if(arr1[i]>arr2[j])
j++ ;
else
{
cout<<arr1[i]<<" " ;
i++ ;
j++ ;
}
}
cout<<endl ;
}
int sizeTree(Node *root)
{
if(!root)
return 0 ;
return 1+sizeTree(root->left)+sizeTree(root->right) ;
}
void Inorder(Node *root,int arr[],int *curr)
{
if(root)
{
Inorder(root->left,arr,&(*curr)) ;
arr[(*curr)++]=root->data ;
Inorder(root->right,arr,&(*curr)) ;
}
}
--------------------------------------------------------------------------------
Comments
Post a Comment