力扣热题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
相关推荐
张3蜂1 小时前
Gunicorn深度解析:Python WSGI服务器的王者
服务器·python·gunicorn
rayufo6 小时前
【工具】列出指定文件夹下所有的目录和文件
开发语言·前端·python
数研小生7 小时前
构建命令行单词记忆工具:JSON 词库与艾宾浩斯复习算法的完美结合
算法·json
芒克芒克7 小时前
LeetCode 题解:除自身以外数组的乘积
算法·leetcode
Python 老手8 小时前
Python while 循环 极简核心讲解
java·python·算法
@Aurora.8 小时前
优选算法【专题九:哈希表】
算法·哈希算法·散列表
爱看科技8 小时前
微美全息(NASDAQ:WIMI)研究拜占庭容错联邦学习算法,数据安全与隐私保护的双重保障
算法
qq_417129258 小时前
C++中的桥接模式变体
开发语言·c++·算法
开源技术8 小时前
如何将本地LLM模型与Ollama和Python集成
开发语言·python
weixin_437044648 小时前
Netbox批量添加设备——堆叠设备
linux·网络·python