Check if two trees are Mirror
PROBLEM :
Given a Two Binary Trees, write a function that returns true if one is mirror of other, else returns false.
Input:
The task is to complete the method that takes two arguments, roots of two Binary Trees to be checked for mirror.
Output:
The function should return true if first tree is mirror of second.
Constraints:
1 <=T<= 30
1 <= Number of nodes<= 100
1 <= Data of a node<= 1000
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/* Node structure
struct Node
{
int data;
struct Node* left, *right;
}; */
/* Given two trees, should return true if they are
mirror of each other. */
int areMirror(Node* a, Node* b)
{
if(a==NULL&&b==NULL)
return 1 ;
if(a==NULL||b==NULL)
return 0 ;
if(a->data==b->data&&areMirror(a->left,b->right)&&areMirror(a->right,b->left))
return 1 ;
return 0 ;
}
---------------------------------------------------------------------------------
Given a Two Binary Trees, write a function that returns true if one is mirror of other, else returns false.
Input:
The task is to complete the method that takes two arguments, roots of two Binary Trees to be checked for mirror.
Output:
The function should return true if first tree is mirror of second.
Constraints:
1 <=T<= 30
1 <= Number of nodes<= 100
1 <= Data of a node<= 1000
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/* Node structure
struct Node
{
int data;
struct Node* left, *right;
}; */
/* Given two trees, should return true if they are
mirror of each other. */
int areMirror(Node* a, Node* b)
{
if(a==NULL&&b==NULL)
return 1 ;
if(a==NULL||b==NULL)
return 0 ;
if(a->data==b->data&&areMirror(a->left,b->right)&&areMirror(a->right,b->left))
return 1 ;
return 0 ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment