判断是否是对称二叉搜索树

问题:

# 给定一个二叉树,检查它是否是镜像对称的。 
#
#
#
# 例如,二叉树 [1,2,2,3,4,4,3] 是对称的。

方法:递归

复制代码
# leetcode submit region begin(Prohibit modification and deletion)
# 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: TreeNode) -> bool:
        def isSameTree(p, q):
            if not p and not q:
                return True
            if p and q and p.val == q.val:
                l = isSameTree(p.left, q.right)
                r = isSameTree(p.right, q.left)
                return l and r
            else:
                return False

        if not root:
            return True
        else:
            return isSameTree(root.left, root.right)
# leetcode submit region end(Prohibit modification and deletion)
复制代码

 

posted @   今夜无风  阅读(36)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
历史上的今天:
2018-09-03 MySQL数据的查询注意
2018-09-03 Python使用pyMysql模块插入数据到mysql的乱码解决
2018-09-03 单元测试
点击右上角即可分享
微信分享提示