力扣热题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
相关推荐
moonsims13 分钟前
Z3 Technology-适用于无人机和机器人的 4K 高清摄像机和视频编码器
算法
摘星编程22 分钟前
深入浅出 Tokio 源码:掌握 Rust 异步编程的底层逻辑
网络·算法·rust·系统编程·tokio
Python×CATIA工业智造28 分钟前
Pycatia二次开发基础代码解析:组件识别、选择反转与链接创建技术解析
python·pycharm
小宁爱Python1 小时前
从零搭建 RAG 智能问答系统 6:Text2SQL 与工作流实现数据库查询
数据库·人工智能·python·django
Hard_Liquor1 小时前
Datawhale秋训营-“大运河杯”数据开发应用创新大赛
人工智能·深度学习·算法
liu****1 小时前
笔试强训(八)
开发语言·算法·1024程序员节
草莓工作室1 小时前
数据结构14:查找
数据结构·算法
m0_748241231 小时前
Java注解与反射实现日志与校验
java·开发语言·python
可触的未来,发芽的智生2 小时前
追根索源:换不同的词嵌入(词向量生成方式不同,但词与词关系接近),会出现什么结果?
javascript·人工智能·python·神经网络·自然语言处理
hu_nil2 小时前
LLMOps-第十一周作业
python·vllm