Why does my answer get a Output Limit Exceeded error?


  • 0
    from collections import deque
    class Solution(object):
        def levelOrder(self, root):
            """
            :type root: TreeNode
            :rtype: List[List[int]]
            """
            if root is None:
                return []
    
            res = []
    
            queue = deque()
            p = root
            queue.append(p)
    
            while queue:
                s = len(queue)
                level_nodes = []
                while s:
                    p = queue.popleft()
                    if p.left:
                        queue.append(p.left)
                    if p.right:
                        queue.append(p.right)
                    s -= 1
                    level_nodes.append(p)
                res.append(level_nodes)
            return res
    

    Thank you very much!


  • 1
    N

    Because you are returning wrong types, if you look at rtype in the docstring then you can see that it is supposed to be List[List[int]] but in your case it is List[List[TreeNode]]


  • 0

    Yes that's the problem. Thank you very much!!!


Log in to reply
 

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