leetcode write up for 91-120 problems
100. Same Tree
base case 都为空返回true。然后用迭代来做。
class Solution(object):
def isSameTree(self, p, q):
return (not p and not q) or (p and q and p.val == q.val and self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right))
101. Symmetric Tree
递归的方法,不然会写的很繁琐。有点难。
class Solution:
def isSymmetric(self, root):
if root==None:
return(True)
p1 = root.left
p2 = root.right
return(self.traverse(p1,p2))
def traverse(self,p1,p2):
if p1==None and p2==None:
return(True)
elif p1==None or p2==None:
return(False)
x1 = self.traverse(p1.left,p2.right)
x2 = self.traverse(p1.right,p2.left)
if x1 & x2 and p1.val==p2.val:
return(True)
return(False)