top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to find if a binary tree is mirror image of another binary tree?

+4 votes
830 views

Code example will help.

posted Oct 21, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+3 votes
boolean tree_compare (tree *roort1, tree *root2)
{
    if ((root1 == NULL) && (root2 == NULL))
    {
        return TRUE;
    }
    else if (root1 && root2)
    {
        if(root1->data == root2->data)
        {
            return (tree_compare(roort1->right, root2->left) && tree_compare(root1->left, roort2->right))
        }
        else
            return FALSE;
    }
    else
        FALSE;
}
answer Oct 21, 2013 by Vikas Upadhyay
–2 votes

A binary tree is a mirror image of itself if its left and right subtrees are identical mirror images i.e., the binary tree is symmetrical.

boolean isMirror(BTree tree) {
  foreach (List<Integer> row : tree.rows() {
    if (row != row.reverse()) return false;
  }
  return true;
}
answer Oct 21, 2013 by Atul Mishra
...