Check if two trees are Mirror | A binary tree node ; * Given two trees , return true if they are mirror of each other ; Base case : Both empty ; If only one is empty ; * Both non - empty , compare them recursively Note that in recursive calls , we * pass left of one tree and right of other tree ; Driver code to test above methods
class Node { Node ( data ) { this . data = data ; this . left = this . right = null ; } } var a , b ; function areMirror ( a , b ) { if ( a == null && b == null ) return true ; if ( a == null b == null ) return false ; return a . data == b . data && areMirror ( a . left , b . right ) && areMirror ( a . right , b . left ) ; } a = new Node ( 1 ) ; b = new Node ( 1 ) ; left = new Node ( 2 ) ; right = new Node ( 3 ) ; left . left = new Node ( 4 ) ; left . right = new Node ( 5 ) ; left = new Node ( 3 ) ; right = new Node ( 2 ) ; right . left = new Node ( 5 ) ; right . right = new Node ( 4 ) ; if ( areMirror ( a , b ) == true ) document . write ( " " ) ; else document . write ( " " ) ;