剑指 Offer 28. 对称的二叉树(递归)

题目
在这里插入图片描述
递归找当前左右结点的值是否相等 并且当前左结点的左结点是否等于当前右结点的右节点 并且 当前左节点的右节点等于当前右节点的左节点

    public Boolean recur(TreeNode left ,TreeNode right){
    
    
        if (left == null && right == null) return true;
        if (left == null || right == null) return false;
        return ((left.val == right.val) && recur(left.left,right.right) && recur(left.right,right.left));
    }
    public boolean isSymmetric(TreeNode root) {
    
    
        if (root == null) return true;
        return recur(root.left,root.right);
    }

猜你喜欢

转载自blog.csdn.net/qq_43434328/article/details/115138823
今日推荐