101. Symmetric Tree
题目:
https://leetcode.com/problems/symmetric-tree/
难度:
Easy
两棵树symmetric, 有几种可能:
- 均为none ,symmetric
- 左孩子,右孩子都不存在,并且值相等, symmetric
- 右子树 和 另一棵树的左子树相等,左子树 和另一颗树的右子树相等 🌲
class Solution(object):
def isSymmetric(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if root == None :
return True
return self.symmetric(root.left, root.right)
def symmetric(self,t1, t2):
# empty tree
if t1 == None or t2 == None:
if t1 == t2 == None:
return True
else:
return False
elif t1.val == t2.val:
return self.symmetric(t1.left,t2.right) and self.symmetric(t1.right, t2.left)
else:
return False
其实函数不应该定义为symmetric,应该定义为qual?更合适。