给定一个二叉树,检查它是否是镜像对称的。

    例如,二叉树 [1,2,2,3,4,4,3] 是对称的。

    1. 1

    / \ 2 2 / \ / \ 3 4 4 3

    但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:

    1. 1

    / \ 2 2 \ \ 3 3

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/symmetric-tree

    1. /**
    2. * Definition for a binary tree node.
    3. * public class TreeNode {
    4. * int val;
    5. * TreeNode left;
    6. * TreeNode right;
    7. * TreeNode(int x) { val = x; }
    8. * }
    9. */
    10. class Solution {
    11. public boolean isSymmetric(TreeNode root) {
    12. return check(root,root);
    13. }
    14. public boolean check(TreeNode root1, TreeNode root2){
    15. if(root1 == null && root2 == null){
    16. return true;
    17. }else if(root1 == null || root2 == null){
    18. return false;
    19. }
    20. return root1.val == root2.val && check(root1.left,root2.right) && check(root1.right,root2.left);
    21. }
    22. }