# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def isSameTree(self, p, q):
"""
:type p: TreeNode
:type q: TreeNode
:rtype: bool
"""
if p==None and q==None:
return True
elif p!=None and q!=None:
pass
else:
return False
if p.val!=q.val:
return False
if p.left!=None and q.left!=None:
if self.isSameTree(p.left,q.left)==False:
return False
elif p.left==None and q.left==None:
pass
else:
return False
if p.right!=None and q.right!=None:
if self.isSameTree(p.right,q.right)==False:
return False
elif p.right==None and q.right==None:
pass
else:
return False
return True
Long Long Python solution..


@TerpsX said in Long Long Python solution..:
remove that
print p.val,q.val
Yeah, go ahead and do that :)
(if you don't know how: click the tripledot icon of the post you want to edit)


You can remove a lot of checks and pass statements. Also it can be done in one line.
def isSameTree(self, p, q): if p is None and q is None: return True if p is not None and q is not None: c1 = p.val == q.val c2 = self.isSameTree(p.left, q.left) c3 = self.isSameTree(p.right, q.right) return c1 and c2 and c3 return False def isSameTree(self, p, q): return p is None and q is None or p is not None and q is not None and (p.val == q.val) and ( self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right))