【剑指offer】对称的二叉树【python】
请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def sysmmetrical(self, root1, root2):
if root1 == None and root2 == None:
return True
if root1 == None or root2 == None:
return False
if root1.val == root2.val:
return self.sysmmetrical(root1.left, root2.right) and self.sysmmetrical(root1.right, root2.left)
else:
return False
def isSymmetrical(self, pRoot):
# write code here
return self.sysmmetrical(pRoot, pRoot)