Easy
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following [1,2,2,null,3,null,3] is not:
1
/ \
2 2
\ \
3 3
1ms:
public boolean isSymmetric(TreeNode root) {
if(root==null)
return true;
return isSym(root.left,root.right);
}
private boolean isSym(TreeNode n1, TreeNode n2){
if(n1==null&&n2==null)
return true;
else if((n1==null&&n2!=null)||(n1!=null&&n2==null))
return false;
if(n1.val==n2.val&&isSym(n1.left,n2.right)&&isSym(n1.right,n2.left))
return true;
else
return false;
}