- 给定一棵二叉树,判断它是不是镜像对称的。 比如:
![1/ \2 2 / \ / \ 3 4 4 3](https://i-blog.csdnimg.cn/blog_migrate/731936ced51008829de34b495cef7ff7.png)
这是镜像对称的
![1 / \ 2 2 \ \ 3 3](https://i-blog.csdnimg.cn/blog_migrate/74b3f2e589a50425267968d1835180f3.png)
这不是镜像对称的public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x; }
}
class Solution {
public boolean isSymmetric(TreeNode root) {
return isMirrorSymmetry(root,root);
}
public boolean isMirrorSymmetry(TreeNode tree1, TreeNode tree2) {
if (tree1 == null && tree2 == null) {
return true;
}
if (tree1 == null || tree2 == null ) {
return false;
}
return tree1.val == tree2.val && isMirrorSymmetry(tree1.left, tree2.right) && isMirrorSymmetry(tree1.right, tree2.left);
}
}