力扣热题100之对称二叉树

题目

给你一个二叉树的根节点 root , 检查它是否轴对称。

代码

方法一:递归

bash 复制代码
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isSymmetric(self, root: Optional[TreeNode]) -> bool:
        def check(left,right):
            if left is None and right is None:
                return True
            if left is None or right is None:
                return False
            if left.val!=right.val:
                return False
            out=check(left.left,right.right)
            inner=check(left.right,right.left)
            return out and inner
        return check(root.left,root.right) if root else True
      

方法二:队迭代

这里用的两个队分别存放根节点的左右两个子树的节点,当然也可以只使用一个队

bash 复制代码
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isSymmetric(self, root: Optional[TreeNode]) -> bool:
        if root is None:
            return True
        queue1=deque([root.left])
        queue2=deque([root.right])
        while queue1 and queue2:
            n=len(queue1)
            for _ in range(n):
                node1=queue1.popleft()
                node2=queue2.popleft()
                if not node1 and not node2:
                    continue
                if not node1 or not node2:
                    return False               
                if node1.val != node2.val :
                    return False            
                queue1.append(node1.left)
                queue1.append(node1.right)
                queue2.append(node2.right)
                queue2.append(node2.left)
        return True

只使用一个队的方法:

bash 复制代码
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isSymmetric(self, root: Optional[TreeNode]) -> bool:
        if root is None:
            return True

        queue=deque()        
        queue.append(root.left)
        queue.append(root.right)
        while queue:
            left=queue.popleft()
            right=queue.popleft()
            
            if not left and not right:
                continue
            if not left or not right:
                return False
            if left.val !=right.val:
                return False
            queue.append(left.left)
            queue.append(right.right)
            queue.append(left.right)
            queue.append(right.left)
        return True
相关推荐
矛取矛求15 分钟前
日期类的实现
开发语言·c++·算法
大翻哥哥27 分钟前
Python 2025:AI工程化与智能代理开发实战
开发语言·人工智能·python
站大爷IP36 分钟前
Python文件处理:从基础操作到实战技巧全解析
python
在下雨5991 小时前
项目讲解1
开发语言·数据结构·c++·算法·单例模式
再努力"亿"点点1 小时前
Sklearn(机器学习)实战:鸢尾花数据集处理技巧
开发语言·python
Jayyih1 小时前
嵌入式系统学习Day36(简单的网页制作)
学习·算法
费弗里1 小时前
无需云服务器!通过Plotly Cloud免费快捷部署Dash应用
python·dash
跟橙姐学代码1 小时前
轻松搞定 Python 模块与包导入:新手也能秒懂的入门指南
前端·python·ipython
荏苒追寻1 小时前
Python 爬虫——爬虫基础
python
脑洞代码1 小时前
20250909的学习笔记
算法