題目描述
請實(shí)現(xiàn)一個(gè)函數(shù),用來判斷一顆二叉樹是不是對稱的。注意,如果一個(gè)二叉樹同此二叉樹的鏡像是同樣的,定義其為對稱的。
# -*- coding:utf-8 -*-
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def isSymmetrical(self, p):
# write code here
def same(p1,p2):
if not p1 and not p2: return True
if not p1 or not p2: return False
return p1.val==p2.val and same(p1.left,p2.right) and same(p1.right,p2.left)
if not p: return True
return same(p.left, p.right)