Constructing both the trees .
Node root1 = new Node ( 1 ) ; root1 . left = new Node ( 3 ) ; root1 . right = new Node ( 2 ) ; root1 . right . left = new Node ( 5 ) ; root1 . right . right = new Node ( 4 ) ; Node root2 = new Node ( 1 ) ; root2 . left = new Node ( 2 ) ; root2 . right = new Node ( 3 ) ; root2 . left . left = new Node ( 4 ) ; root2 . left . right = new Node ( 5 ) ; System . out . println ( areAnagrams ( root1 , root2 ) ? " Yes " : " No " ) ; } }