题目描述
给定一个二叉树,检查它是否是镜像对称的。
示例

题解
太简单了,自己看代码。
代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {
return isMirror(root.left, root.right);
}
public boolean isMirror(TreeNode left, TreeNode right) {
if (left == null && right == null) {
return true;
}
if (left == null || right == null) {
return false;
}
return (left.val == right.val) && isMirror(left.left, right.right) && isMirror(left.right, right.left);
}
}
性能

该博客介绍了一种方法来检查给定的二叉树是否是对称的。通过递归地比较二叉树的左右子节点,如果左子树与右子树的镜像相等,则树是对称的。提供的Java代码实现简洁明了,通过isMirror函数进行判断,确保在所有层级上节点都保持相同的值和镜像结构。

被折叠的 条评论
为什么被折叠?



