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

本文介绍了两种方法来判断一棵二叉树是否是对称的。第一种方法涉及构造翻转二叉树并进行比较,第二种方法则通过调整先序遍历的方式直接比较左右子树。每种方法都包含详细的代码实现,展示了如何递归地检查树的对称性。这两种高效算法对于理解和处理二叉树问题具有实践价值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述
两种思路:

思路1

建立一个翻转的二叉树newroot ,然后比较两个二叉树是都相同。
翻转代码:

TreeNode jing(TreeNode root)
    {
        if(root == null || (root.left == null && root.right == null))
        return root;
        TreeNode newroot = new TreeNode(root.val);
        newroot.left = jing(root.right);
        newroot.right = jing(root.left);
        return newroot;
    }

比较相同代码:

boolean cur(TreeNode A , TreeNode B)
    {
        if((A == null && B != null) || (B == null && A != null))
          return false;
        if(A == null && B == null)
        return true;
        else
        return A.val == B.val && cur(A.left, B.left) && cur(A.right ,B.right);
    }

总体代码:

/**
 * 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 || (root.left == null && root.right == null))
        return true;
        TreeNode newroot = jing(root);
        return cur(newroot ,root);
    }
    TreeNode jing(TreeNode root)
    {
        if(root == null || (root.left == null && root.right == null))
        return root;
        TreeNode newroot = new TreeNode(root.val);
        newroot.left = jing(root.right);
        newroot.right = jing(root.left);
        return newroot;
    }
    boolean cur(TreeNode A , TreeNode B)
    {
        if((A == null && B != null) || (B == null && A != null))
          return false;
        if(A == null && B == null)
        return true;
        else
        return A.val == B.val && cur(A.left, B.left) && cur(A.right ,B.right);
    }
}

效果还可以:
在这里插入图片描述

思路2

可以不建立翻转二叉树,只需要将root树以两种不同的方式遍历即可,左树使用先序遍历,即中-》左-》右;
右树使用先序反遍历,即中-》右-》左;
只需要修改上思路1 的cur函数即可:

boolean cur(TreeNode A , TreeNode B)
    {
        if((A == null && B != null) || (B == null && A != null))
          return false;
        if(A == null && B == null)
        return true;
        else
        return A.val == B.val && cur(A.left, B.right) && cur(A.right ,B.left);
    }

如果为空节点或者叶子节点返回,否则比较A树的左孩子和右树的右孩子。
代码如下:

/**
 * 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) {
        return cur(root ,root);
    }
    boolean cur(TreeNode A , TreeNode B)
    {
        if((A == null && B != null) || (B == null && A != null))
          return false;
        if(A == null && B == null)
        return true;
        else
        return A.val == B.val && cur(A.left, B.right) && cur(A.right ,B.left);
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大鱼qss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值