
思路:递归,左子树和右子树是否相等
public boolean isSymmetric(TreeNode root) {
return isMirror(root, root);
}
public boolean isMirror(TreeNode t1, TreeNode t2) {
if (t1 == null && t2 == null) {
return true;
}
if (t1 == null || t2 == null) {
return false;
}
if (t1.val == t2.val) {
return isMirror(t1.left, t2.right) && isMirror(t1.right, t2.left);
}
return false;
}
网友评论