对称的二叉树 牛客网 剑指Offer

版权声明:版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/DarrenXf/article/details/82497060

对称的二叉树 牛客网 剑指Offer

  • 题目描述
  • 请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    def isSymmetrical(self,pRoot):
        return self.selfIsSymmetrical(pRoot,pRoot)
    def selfIsSymmetrical(self,pRoot1,pRoot2):
        if pRoot1 == None and pRoot2 == None:
            return True
        if pRoot1 == None or pRoot2 == None:
            return False
        if pRoot1.val != pRoot2.val:
            return False
        return self.selfIsSymmetrical(pRoot1.left,pRoot2.right) and self.selfIsSymmetrical(pRoot1.right,pRoot2.left)

猜你喜欢

转载自blog.csdn.net/DarrenXf/article/details/82497060