top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Check if two tree are mirror?

0 votes
275 views
Check if two tree are mirror?
posted May 30, 2016 by anonymous

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

1 Answer

0 votes
/* Given two trees, return true if they are
   mirror of each other */
int areMirror(Node* a, Node* b)
{
    /* Base case : Both empty */
    if (a==NULL && b==NULL)
        return true;

    // If only one is empty
    if (a==NULL || b == NULL)
        return false;

    /* Both non-empty, compare them recursively
     Note that in recursive calls, we pass left
     of one tree and right of other tree */
    return  a->data == b->data &&
            areMirror(a->left, b->right) &&
            areMirror(a->right, b->left);
}
answer May 30, 2016 by Rajan Paswan
Similar Questions
+6 votes
                    50                      50
                   /  \                    /  \
                  20     30              30   20
Sample Tree<---   /  \                       /  \   ----> Mirror image
               70      80                   80   70
              /  \    \                    /    /  \  
             10  40     60                60   40   10
+1 vote

For example

Input : Binary Tree

               A
             /    \ 
           B        E
         /   \       \    
        C     D       B     
                     /  \    
                    C    D

Output : Yes

+2 votes

Binary tree need to fulfill following two conditions for being a heap –
* It should be a complete tree (i.e. all levels except last should be full).
* Every node’s value should be greater than or equal to its child node (considering max-heap).

C/C++ code would be helpful?

...