true
if they are, false
otherwise. What's a binary tree's mirror image? Hold it by the root and rotate all other nodes by 180 degrees!
Example: 1 1 / \ / \ 2 3 == 3 2 / \ / \ / \ / \ 4 5 6 7 7 6 5 4
1. The recursive base condition is : if both the roots are null
, return true
.
2. If either one of them is null
, return false
.
3. If the data of the root node of the first tree is not equal to the data of the root node of second tree, return false
.
4. Check if the left subtree of the first tree is the mirror image of the right subtree of the second tree - isMirror(root1.left, root2.right)
.
5. Check if the right subtree of the first tree is the mirror image of the left subtree of the second tree - isMirror(root1.right, root2.left)
.
public boolean isMirror(TreeNode root1, TreeNode root2) { }
C
Java
Python