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):
        def isMirror(left, right):
            if left==None and right==None:
                return True
            elif left==None or right==None:
                return False

            if left.val!=right.val:
                ret1=isMirror(left.left, right.right)
                ret2=isMirror(left.right, right.left)
                return ret1 and ret2

            if pRoot == None:
                return True

            return isMirror(pRoot.left, pRoot.right)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章