Login light

leetcode/101. 对称二叉树.md

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def isSymmetric(self, root: TreeNode) -> bool:
        if None == root:
            return True
        if None == root.left and None == root.right:
            return True
        if (None == root.left and None != root.right) or (None != root.left and None == root.right):
            return False
        return self.isSymmetricTree(root.left,root.right)

    def isSymmetricTree(self, p: TreeNode, q: TreeNode) -> bool:
        if None == p and None == q:
            return True
        if (None == p and None != q) or (None != p and None == q):
            return False
        if p.val == q.val:
            return self.isSymmetricTree(p.left,q.right) and self.isSymmetricTree(p.right,q.left)
        else:
            return False