class Solution {
public boolean isSymmetric(TreeNode root) {
if(root==null){
return true;
}
return isSame(inverse(root.left),root.right);
}
//反转二叉树
public TreeNode inverse(TreeNode root){
if(root==null){
return null;
}
inverse(root.left);
inverse(root.right);
TreeNode p;
p=root.left;
root.left=root.right;
root.right=p;
return root;
}
//判断两树是否相等
public boolean isSame(TreeNode n1,TreeNode n2){
if(n1==null&&n2==null){
return true;
}
if(n1==null||n2==null){
return false;
}
if(n1.val!=n2.val){
return false;
}
if(isSame(n1.left,n2.left)&&isSame(n1.right,n2.right)){
return true;
}
return true;
}
}
39行应该return false吧
你只需要检查数组的下标就可以了。
1
2 3
4 5 6 7