6line AC python


  • 35
    S
        def isSymmetric(self, root):
            def isSym(L,R):
                if not L and not R: return True
                if L and R and L.val == R.val: 
                    return isSym(L.left, R.right) and isSym(L.right, R.left)
                return False
            return isSym(root, root)

  • 8

    I simplify your isSym funciton:

    def isSym(L,R):
        if L and R and L.val == R.val: 
            return isSym(L.left, R.right) and isSym(L.right, R.left)
        return L == R
    

    My Iterative solution.

    def isSymmetric(self, root):
        queue = [root]
        while queue:
            values = [i.val if i else None for i in queue]
            if values != values[::-1]: return False
            queue = [child for i in queue if i for child in (i.left, i.right)]
        return True

  • 0
    L

    @lee215 Pretty. Thanks, really learned a lot from your codes.


  • 0
    S

    @lee215
    Can you explain "queue = [child for i in queue if i for child in (i.left, i.right)]"
    I do not know the means of "if i" above


  • 0

    @SmileJohnson
    if i != None


Log in to reply
 

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