Three lines simple Python solution

  • 0
    if not (p or q): return True
    if not (p and q) or p.val != q.val: return False
    return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)

    Note: not (p and q) after not (p or q) block is XOR

Log in to reply

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