【剑指Offer】个人学习笔记_28_对称的二叉树

刷题日期:19:1838 星期二2021年4月6日

个人刷题记录,代码收集,来源皆为leetcode

经过多方讨论和请教,现在打算往Java方向发力

主要答题语言为Java

题目:

剑指 Offer 28. 对称的二叉树

难度简单157

请实现一个函数,用来判断一棵二叉树是不是对称的。如果一棵二叉树和它的镜像一样,那么它是对称的。

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

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

  1  / \ 2  2  \  \  3   3

示例 1:

输入:root = [1,2,2,3,4,4,3]
输出:true

示例 2:

输入:root = [1,2,2,null,3,null,3]
输出:false

限制:

0 <= 节点个数 <= 1000
题目分析

树类型的题还是得用递归,取巧的话感觉不太好弄。

节点数也有所限制,题目还声明是二叉树。

初始解答:

只能解决示例问题的初始版本

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isSymmetric(TreeNode root) {
        if (root == null) return false; //边界条件
        return isJ(root.left, root.right);
        // return false;
    }
    public boolean isJ(TreeNode rootl, TreeNode rootr) {
        if (rootl.left == null && rootr.right == null && rootr.left == null && rootl.right == null) return true;
        if (rootl.left == rootr.right && rootr.left == rootl.right) return isJ(rootl.left, rootl.right) && isJ(rootr.left, rootr.right);
        return true;
    }
}

参考了方法一和二之后,思路就清晰许多了。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isSymmetric(TreeNode root) {
        if (root == null) return true; //边界条件
        return isJ(root.left, root.right);
        // return false;
    }
    public boolean isJ(TreeNode rootl, TreeNode rootr) {
        if (rootl == null && rootr == null) return true;
        if (rootl == null || rootr == null) return false;
        return rootl.val == rootr.val && isJ(rootl.left, rootr.right) && isJ(rootr.left, rootl.right);
    }
}

执行结果: 通过

显示详情

执行用时:0 ms, 在所有 Java 提交中击败了100.00%的用户

内存消耗:36.3 MB, 在所有 Java 提交中击败了80.52%的用户

说明思考的方向是对的。

参考K神方法四精简代码

class Solution {
    public boolean isSymmetric(TreeNode root) {
        return root == null ? true : isJ(root.left, root.right); //条件表达式
    }
    public boolean isJ(TreeNode rootl, TreeNode rootr) {
        if (rootl == null && rootr == null) return true;
        if (rootl == null || rootr == null || rootl.val != rootr.val) return false;
        return isJ(rootl.left, rootr.right) && isJ(rootr.left, rootl.right);
    }
}

学习他人:

方法一:

Tai_ParkL1 2020-04-02

做递归思考三步:

  1. 递归的函数要干什么?
  • 函数的作用是判断传入的两个树是否镜像。
  • 输入:TreeNode left, TreeNode right
  • 输出:是:true,不是:false
  1. 递归停止的条件是什么?
  • 左节点和右节点都为空 -> 倒底了都长得一样 ->true
  • 左节点为空的时候右节点不为空,或反之 -> 长得不一样-> false
  • 左右节点值不相等 -> 长得不一样 -> false
  1. 从某层到下一层的关系是什么?
  • 要想两棵树镜像,那么一棵树左边的左边要和二棵树右边的右边镜像,一棵树左边的右边要和二棵树右边的左边镜像
  • 调用递归函数传入左左和右右
  • 调用递归函数传入左右和右左
  • 只有左左和右右镜像且左右和右左镜像的时候,我们才能说这两棵树是镜像的
  1. 调用递归函数,我们想知道它的左右孩子是否镜像,传入的值是root的左孩子和右孩子。这之前记得判个root==null。

方法二:

洪L1 (编辑过)2020-04-03

class Solution {
    public boolean isSymmetric(TreeNode root) {
        if (root == null)
            return true;
        return helper(root.left, root.right);
    }

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

方法三:

中国好胖子 2020-12-13 和上面的子函数差了一点

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isSymmetric(TreeNode root) {
        if (root == null) return true;
        return isSymmetric(root.left,root.right);

    }
    private boolean isSymmetric(TreeNode s,TreeNode t){
        if (s!= null && t!= null){
            return s.val == t.val && isSymmetric(s.left,t.right) && isSymmetric(s.right,t.left);
        }
        else return s == null && t == null;
    }
}

方法四:

K神 作者:jyd
链接:https://leetcode-cn.com/problems/dui-cheng-de-er-cha-shu-lcof/solution/mian-shi-ti-28-dui-cheng-de-er-cha-shu-di-gui-qing/
来源:力扣(LeetCode)

class Solution {
    public boolean isSymmetric(TreeNode root) {
        return root == null ? true : recur(root.left, root.right);
    }
    boolean recur(TreeNode L, TreeNode R) {
        if(L == null && R == null) return true;
        if(L == null || R == null || L.val != R.val) return false;
        return recur(L.left, R.right) && recur(L.right, R.left);
    }
}

总结

以上就是本题的内容和学习过程了,递归是主流解决树问题的方法,要熟练。

欢迎讨论,共同进步。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值