Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root==null){
return true;
}
return helper(root.left,root.right);
}
private boolean helper(TreeNode n1,TreeNode n2){
if(n1==null&&n2==null){
return true;
}
else if(n1==null||n2==null){
return false;
}
else if(n1.val==n2.val){
return helper(n1.left,n2.right)&&helper(n1.right,n2.left);
}
else{return false;}
}
}