python 39ms solution

  • 0
    def isSameTree(self, p,q):
        if not p and not q:
            # two empty roots
            return True
        elif (not p and q) or (not q and p):
            # one node is empty but another one is not
            return False 
        elif p.val != q.val:
            return False
            # p and q are equal
            return (self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right))

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.