LeetCode 热题100-39 对称二叉树

对称二叉树

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

示例 1:

复制代码
输入:root = [1,2,2,3,4,4,3]
输出:true

示例 2:

复制代码
输入:root = [1,2,2,null,3,null,3]
输出:false

提示:

  • 树中节点数目在范围 [1, 1000]
  • -100 <= Node.val <= 100

**进阶:**你可以运用递归和迭代两种方法解决这个问题吗?

emmm...

用了递归,对于树的题目能用递归则用递归...qwq

就是对称着去判断,代码可能看着长而繁...(越来越觉得内置函数好用了...

python 复制代码
# 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:
        real = True
        def func(left,right):
            if left!=None and right== None or left==None and right!=None:return False
            if left and right and left.val != right.val:return False
            if left and right:return func(left.left,right.right) and func(left.right,right.left)
            else:return True
        if root == None:return True
        return func(root.left,root.right)
相关推荐
aqi0011 小时前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵13 小时前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf14 小时前
Agent 流程编排
后端·python·agent
copyer_xyf14 小时前
Agent RAG
后端·python·agent
copyer_xyf15 小时前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf15 小时前
Agent 记忆管理
后端·python·agent
JieE2121 天前
LeetCode 56. 合并区间|超清晰 JS 图解思路,面试高频区间题
javascript·算法·面试
星云穿梭1 天前
用Python写一个带图形界面的学生管理系统——完整教程
python
Jack201 天前
HarmonyOS开发中错误处理策略:网络异常统一处理
算法