对称的二叉树-python

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    def isSymmetrical(self, pRoot):
        # write code here
        if not pRoot:
            return True
        return self.contrast(pRoot.left, pRoot.right)
    
    def contrast(self, p1, p2):
        if not p1 and not p2:
            return True
        if not p1 or not p2:
            return False
        if p1.val == p2.val:
            return self.contrast(p1.left, p2.right) and self.contrast(p1.right, p2.left)
        else:
            return False
posted @ 2019-08-12 16:37  Dolisun  阅读(147)  评论(0编辑  收藏  举报