Use recursion.
Solution:
# T:O(n) S:O(h)
class Solution:
# @param root, a tree node
# @return a boolean
def help(self, p, q):
if p == None and q == None: return True
if p and q and p.val == q.val:
return self.help(p.right, q.left) and\
self.help(p.left, q.right)
return False
def isSymmetric(self, root):
if root:
return self.help(root.left, root.right)
return True
Run Time: 72 ms
No comments:
Post a Comment