对称二叉树

1.介绍

 

2.解决思想

递归方式实现

3.代码

public static boolean isSymmetric(TreeNode root) {
    if (root == null) return true;
    return isSymmetric2(root.left, root.right);
}

public static boolean isSymmetric2(TreeNode root1, TreeNode root2) {
    if (root1 == null && root2 == null) return true;
    if (root1 == null || root2 == null) return false;
    return (root1.val == root2.val) && isSymmetric2(root1.left, root2.right) && isSymmetric2(root1.right, root2.left);
}

 


posted @ 2018-12-18 16:34  夜宵95  阅读(97)  评论(0编辑  收藏  举报