LeetCode-101. 对称二叉树

给定一个二叉树,检查它是否是镜像对称的。

# 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:
        if not root:
            return True

        def issy(a, b):
            if not a and not b:
                return True
            elif a and b and a.val == b.val:
                return issy(a.left, b.right) and issy(a.right, b.left)
            else:
                return False 
        
        return(issy(root.left, root.right))
posted @ 2021-07-16 09:23  小Aer  阅读(2)  评论(0编辑  收藏  举报  来源