剑指offer-对称的二叉树之python实现

root是否为none的判断要放在值的判断之前

class Solution:
    def isSymmetrical(self, pRoot):
        if pRoot == None:
            return True
        return  self.helper(pRoot, pRoot)
        
    def helper(self, root1, root2):
        if root1 == None and root2 == None:
            return True
        elif root1 == None or root2 == None:
            return False
        elif root1.val != root2.val:
            return False
        
        return self.helper(root1.left, root2.right) and self.helper(root1.right, root2.left)

 

你可能感兴趣的:(算法)