力扣热题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
相关推荐
weixin_307779132 小时前
Redis Windows迁移方案与测试
c++·windows·redis·算法·系统架构
麦兜*4 小时前
Spring Boot集成方案 + Elasticsearch向量检索,语义搜索核弹
java·spring boot·python·spring·elasticsearch·spring cloud·系统架构
仪器科学与传感技术博士4 小时前
python:讲懂决策树,为理解随机森林算法做准备,以示例带学习,通俗易懂,容易理解和掌握
python·算法·决策树
歪歪1004 小时前
HTML 如何转 Markdown
开发语言·chrome·python·程序人生·html
小指纹4 小时前
cf--思维训练
c++·算法·macos·ios·objective-c·cocoa
小指纹4 小时前
河南萌新联赛2025第(四)场【补题】
数据结构·c++·算法·macos·objective-c·cocoa·图论
菜鸟555554 小时前
河南萌新联赛2025第四场-河南大学
c++·算法·思维·河南萌新联赛
王者鳜錸4 小时前
PYTHON从入门到实践-18Django模版渲染
开发语言·python·django
F_D_Z5 小时前
【感知机】感知机(perceptron)模型与几何解释
学习·算法·支持向量机
竹子_235 小时前
《零基础入门AI:传统机器学习进阶(从拟合概念到K-Means算法)》
人工智能·算法·机器学习