力扣热题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
相关推荐
Hcoco_me22 分钟前
RTMPose_JSON相关解读
算法·数据挖掘·json·聚类
高洁0132 分钟前
DNN案例一步步构建深层神经网络(二)
人工智能·python·深度学习·算法·机器学习
Insight.39 分钟前
背包问题——01背包、完全背包、多重背包、分组背包(Python)
开发语言·python
aini_lovee41 分钟前
改进遗传算法求解VRP问题时的局部搜索能力
开发语言·算法·matlab
Lucky高44 分钟前
Pandas库实践1_预备知识准备
python·pandas
老蒋新思维1 小时前
反脆弱性设计:创始人IP与AI智能体如何构建愈动荡愈强大的知识商业|创客匠人
人工智能·网络协议·tcp/ip·算法·机器学习·创始人ip·创客匠人
Salt_07281 小时前
DAY 36 官方文档的阅读
python·算法·机器学习·github
k***92161 小时前
Python 科学计算有哪些提高运算速度的技巧
开发语言·python
superman超哥1 小时前
仓颉条件变量深度解析与实践:解锁高效并发同步
开发语言·python·c#·仓颉
长空任鸟飞_阿康1 小时前
LangGraph 技术详解:基于图结构的 AI 工作流与多智能体编排框架
人工智能·python·langchain