Created
October 23, 2017 20:11
-
-
Save shailrshah/5f310c84fbd3c551dd9d1a3d6a6c8ec2 to your computer and use it in GitHub Desktop.
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public boolean isSymmetric(TreeNode root) { | |
return isSymmetricHelper(root, root); | |
} | |
private static boolean isSymmetricHelper(TreeNode root1, TreeNode root2) { | |
if(root1 == null && root2 == null) | |
return true; | |
if(root1 == null || root2 == null) | |
return false; | |
return root1.val == root2.val && | |
isSymmetricHelper(root1.left, root2.right) && | |
isSymmetricHelper(root1.right, root2.left); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment