题目描述:
    image.png
    image.png
    解析:递归,看根节点左右孩子是否是镜像树

    1. class Solution {
    2. public boolean isSymmetric(TreeNode root) {
    3. if (root == null) return false;
    4. return isMirror(root.left, root.right);
    5. }
    6. private boolean isMirror(TreeNode root1, TreeNode root2) {
    7. if (root1 == null && root2 == null) return true; //说明都已经匹配完成
    8. if (root1 == null || root2 == null) return false; //说明还没有匹配完
    9. if (root1.val != root2.val) return false; //表明匹配错误
    10. return isMirror(root1.left, root2.right) && isMirror(root1.right, root2.left);
    11. }
    12. }