Monthly Archives: November 2016

Two Mirror Trees Checker

Two Mirror Trees Checker

Given a Two Binary Trees, write a function that returns true if one is mirror of other, else returns false.
Algorithm:
  • Traverse tree in preOrder
    • If node1 is null && ndoe2 is null then return tree
    • Check node1.data == node2.data
    • Recursively call with node1.left and node2.right
    • Recursively call with node1.right and node2.left

Latest Source Code:
Github: MirrorBinaryTreeChecker.java


Output:

       1               
      / \       
     /   \      
    /     \     
   /       \    
   2       3       
  / \     / \   
 /   \   /   \  
 4   5   6   7   
/ \ / \ / \ / \ 
8 9 10 11 12 13 14 15 
                                
After reverse : 
       1               
      / \       
     /   \      
    /     \     
   /       \    
   3       2       
  / \     / \   
 /   \   /   \  
 4   5   6   7   
/ \ / \ / \ / \ 
15 14 13 12 11 10 9 8