Created
May 10, 2015 17:32
-
-
Save HDegano/b799d86d82f424f6f7fe to your computer and use it in GitHub Desktop.
Check if Tree is symmetrical
This file contains 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 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