Check If One Binary is Mirror Tree of another Binary Tree.

Objective: – Given two binary trees check if they are mirror image of each other.

Example:

Check-If-One-Binary-is-Mirror-Tree-of-another-Binary-Tree

Check-If-One-Binary-is-Mirror-Tree-of-another-Binary-Tree

Approach:

  • Do the preorder traversal on both the trees simultaneously.
  • if any node doesn’t have corresponding node in the another tree, return false.
  • check if left node in one tree is the right node in another tree, and vice verse.

Code:


Output:

Is Mirror Trees : true

__________________________________________________
Top Companies Interview Questions..-

Google Microsoft Amazon Facebook more..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________

You may also like...

  • sanjib

    add data check for completeness of the program
    if(root1.data !=root2.data){

    return false;

    }

    • tutorialhorizon

      Thanks sanjib, updated it ๐Ÿ™‚

  • Shilpi

    data check should be done after null check

%d bloggers like this: