力扣101.对称二叉树 Java

力扣101.对称二叉树

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

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

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

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

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean isSymmetric(TreeNode root) {
      return isMirror(root,root);
    }
    public boolean isMirror(TreeNode t1, TreeNode t2){
        if(t1==null && t2==null)
        return true;
        if(t1==null || t2 == null)
        return false;
        return (t1.val==t2.val)&&isMirror(t1.right,t2.left)&&isMirror(t1.left,t2.right);
    }
}

你可能感兴趣的:(leetcode,二叉树,leetcode,java,算法)