- package MirrorTree;
-
- /**
- * @author 真他喵的学不动咯
- * @create 2022-08-14--14:41
- */
- public class Mirror { //判断一颗树是不是镜面树
- //https://leetcode.cn/problems/symmetric-tree/
-
- public static class TreeNode{
- public int val;
- public TreeNode left;
- public TreeNode right;
- }
- //用一个方法,传两棵树进去
- public static boolean isSymmetric(TreeNode root){
- return isMirror(root,root);
- }
- public static boolean isMirror(TreeNode h1,TreeNode h2){
- if (h1==null^h2==null){ //h1和h2有一个是空的,直接false
- return false;
- }
- if (h1==null&&h2==null){ //两个都是空的,直接true
- return true;
- }
- return h1.val==h2.val&&isMirror(h1.left,h2.right)&&isMirror(h1.right,h2.left);
- // 头结点不必镜面,必然相同 h1左=h2右 h1右=h2左
- }
- }