I have no idea why I'm failing on what leetcode says is an input of '[]'


  • 0
    B

    Because leetcode will not tell me what it is actually giving to my function, I cannot tell why I am getting a syntax eror. It says its inputting '[]', but since it's supposed to be giving the function a node object, that message is completely meaningless to me. I know that If I give the function a None or an empty list, it will just return a blank list and not error. I have no idea what it is that they're passing in to make it fail. Any help appreciated; thanks in advance!

     class Solution(object):
        def zigzagLevelOrder(self, root):
            ret = []
            if not root:
                return ret
            order = 1
            lvl = [root]
            while len(lvl):
                nlvl = self.grabNextLevel(self, lvl):
                if order == 1:
                    ret.append(lvl)
                    order = 2
                else:
                    ret.append(lvl.reverse())
                    order = 1
                lvl = nlvl
                    
            return ret
            
        def grabNextLevel(self, lvl):
            ret = []
            for node in lvl:
                if node.left:
                    ret.append(node.left)
                if node.right:
                    ret.append(node.right)
            return ret

  • 0

    "Because leetcode will not tell me what it is actually giving to my function, I cannot tell why I am getting a syntax eror."

    This is nonsense. Since you have a syntax error, your code isn't even run at all and thus isn't given anything. Just fix that error. While you kept it secret from us (why?), you are told in which line it is, and it's pretty obvious.

    Anyway, [] means "no nodes", i.e., the empty tree. You'll get None as input.


Log in to reply
 

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