题目
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c7e884695b1aedb686705a196c73e1e8.png)
大致思路
- 判断依据很简单:①根节点相同,②③根节点的左(右)子树的值和根节点的右(左)子树的值相同即可,然后递归调用即可。
- 写的时候注意root为null的情况,使代码完善。
代码实现
package com.immunize.leetcode.isSymmetric;
public class Solution {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public static boolean SubSymmetric(TreeNode root1, TreeNode root2) {
if (root1 == null && root2 == null)
return true;
else if (root1 != null && root2 != null && root1.val == root2.val && SubSymmetric(root1.left, root2.right)
&& SubSymmetric(root1.right, root2.left))
return true;
return false;
}
public boolean isSymmetric(TreeNode root) {
if (root == null)
return true;
return SubSymmetric(root.left, root.right);
}
}