题目
给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
1
/
2 2
/ \ /
3 4 4 3
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
1
/
2 2
\
3 3
代码
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root == null )
return true;
return isSymmetric(root.left,root.right);
}
public boolean isSymmetric(TreeNode L , TreeNode R){
if(L == null && R == null)
return true;
if(L == null || R == null)
return false;
if(L.val == R.val)
return isSymmetric(L.left, R.right) && isSymmetric(L.right, R.left);
return false;
}
}