python LeetCode 刷题记录 101

题目

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

代码

# 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 not root:
            return True
        return self.cheak_lr(root.left, root.right) 
    def cheak_lr(self, left, right):
            if not left and not right:
                return True
            if not left or not right:
                return False
            if left.val != right.val:
                return False
            return self.cheak_lr(left.left, right.right) and self.cheak_lr(left.right, right.left)
    


你可能感兴趣的:(LeetCode,python,leetcode)