My 44ms python solution


  • 2
    H
    class Solution:
        # @param {TreeNode} p
        # @param {TreeNode} q
        # @return {boolean}
        def isSameTree(self, p, q):
            if p==q==None:
                return True
            if None in [p,q]:
                return False
            if p.val!=q.val:
                return False
            return self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right)

  • 3
    C
    Good answer, it seems you can shorten your code as:
    def isSameTree(self, p, q):
        if p and q:
            return p.val == q.val and self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
        else:
            return p == q

  • 0
    H

    you should think about if p == q == None , what about this case?


  • 0
    C

    the "else" clause includes this case. if p == q == None, it will return True.


Log in to reply
 

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