101. Symmetric Tree [对称的树]_剑指offer_面试题28. 对称的二叉树

本文探讨了如何检查一个二叉树是否是镜像对称的,即围绕其中心对称。通过递归和迭代两种方法,详细解析了算法实现,包括Python和Java代码示例。

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

描述

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center)

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

例子
在这里插入图片描述

思路

第100题:两个树是否相同

  1. 比较具有相同的值
  2. 比较结点的左子树和另一个结点的左子树

此题:两个树互为镜像

. 结点具有相同的值
. 比较结点的左子树和另一个节点的右子树

答案

  1. 递归
    python
class Solution:
    def isSymmetric(self, root: TreeNode) -> bool:
        '''
        如此,虽然写着方便,但会多一倍的重复计算 要算 self.check(root.left, root.right) 和self.check(root.right, root.left)
        return self.check(root, root)
        '''
        
        if root: #不为空
            return self.check(root.left,root.right)
        return True #为空
        
        
    def check(self,p,q):
        if not p and not q:
            return True
        if not p or not q or p.val != q.val:
            return False
        return self.check(p.left, q.right) and self.check(p.right, q.left)

java

class Solution {
    public boolean isSymmetric(TreeNode root) {
        /*
        如此,虽然写着方便,但会多一倍的重复计算 要算 check(root.left, root.right) 和check(root.right, root.left)
        return check(root, root)
        */
        if (root != null)
            return check(root.left, root.right);
        return true; //为空  
    }
    
    public boolean check(TreeNode p, TreeNode q)
    {
        if (p == null && q == null)
            return true;
        if (p == null || q == null || p.val != q.val)
            return false;
        return check(p.left, q.right) && check(p.right, q.left);
    }
}
  1. 迭代,使用栈
    python
class Solution:
    def isSymmetric(self, root: TreeNode) -> bool:
        
        if not root:
            return True
        arr=[]
        arr.extend([root.left, root.right])
        
        while len(arr)>0:
            
            p = arr.pop()
            q = arr.pop()
            
            if not q and not p: #如果该两个结点为null,不用比较,直接再取两个
                continue
            if (not q or not p): #有一个时Null
                return False
            # 都不是null
            if p.val != q.val:
                return False
   
            arr.extend([p.left, q.right, p.right,q.left])
      
        return True

java

class Solution {
    public boolean isSymmetric(TreeNode root) {
        
        if (root == null)
            return true;
        
        Stack<TreeNode> s = new Stack<TreeNode>();
        s.push(root.left);
        s.push(root.right);
        
        while (s.size()>0)
        {
            
            TreeNode p = s.pop();
            TreeNode q = s.pop();
        
            if (p==null && q==null) //两个都是NOne
                continue;
            
            if (p==null || q==null) //有一个为None
                return false;
             //两个都不是None
            
            if (p.val != q.val)
                return false;
            s.push(p.left);
            s.push(q.right);
            s.push(p.right);
            s.push(q.left);
            
        }
        return true;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值