小白同学对于二叉树还是不太了解的,作者推荐:二叉树的初步认识_加瓦不加班的博客-CSDN博客
对称二叉树-力扣 101 题


作者给的图:

代码:
java
public boolean isSymmetric(TreeNode root) {
//刚刚开始,传入的是顶堆的左、右两个子叶
return check(root.left, root.right);
}
public boolean check(TreeNode left, TreeNode right) {
// 若同时为 null:也就是整个二叉树就只有一个元素就是顶堆
if (left == null && right == null) {
return true;
}
// 若有一个为 null (有上一轮筛选,另一个肯定不为 null)
if (left == null || right == null) {
return false;
}
//都不为null,则查看值相不相同
if (left.val != right.val) {
return false;
}
return check(left.left, right.right) && check(left.right, right.left);
}
代码解析:

类似题目:Leetcode 100 题 - 相同的树