Failing OJ submission test while same inputs are passing custom test!! Python


  • 0
    R

    1 - Find max length of the tree

    2 - Make storage array

    3 - Append nodes in bottom top manner

    For test case [1,2], it passes the custom test case but fails when I submit it.
    Any idea why?

    class Solution(object):
        res = []
        def levelOrderBottom(self, root):
            """
            :type root: TreeNode
            :rtype: List[List[int]]
            """
            # find max length of the tree
            ml = self.maxLen(root,0)
            # make an empty list of lists
            for i in range(ml):
                self.res.append([])
            # append in bottom top manner
            self.helper(root,0,ml)
            return self.res
            
            
        def maxLen(self,root,level):
            if root == None:
                return level
                
            l = self.maxLen(root.left,level+1)
            r = self.maxLen(root.right,level+1)
            return max(l,r)
            
        def helper(self,present,level,maxi):
            if present == None:
                return level
            self.res[maxi-level-1].append(present.val)
            self.helper(present.left,level+1,maxi)
            self.helper(present.right,level+1,maxi)
            return

Log in to reply
 

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