Skip to content

Instantly share code, notes, and snippets.

@HDegano
Created May 10, 2015 17:32
Show Gist options
  • Save HDegano/b799d86d82f424f6f7fe to your computer and use it in GitHub Desktop.
Save HDegano/b799d86d82f424f6f7fe to your computer and use it in GitHub Desktop.
Check if Tree is symmetrical
public class SymmetricTree{
public boolean isSymmetric(TreeNode root) {
return isSymmetric(root, root);
}
private boolean isSymmetric(TreeNode p, TreeNode q){
if(p == null && q == null) return true;
if(p == null || q == null) return false;
if(p.val != q.val) return false;
return isSymmetric(p.right, q.left) && isSymmetric(p.left, q.right);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment