Skip to content

Instantly share code, notes, and snippets.

@shailrshah
Created October 23, 2017 20:11
Show Gist options
  • Save shailrshah/5f310c84fbd3c551dd9d1a3d6a6c8ec2 to your computer and use it in GitHub Desktop.
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).
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