I wonder why this Python code doesn't pass? Thanks!


  • 0
    C

    Hello everyone! I'm quite confused why the following code doesn't pass. Leetcode says it passes 123/124 which means only the last dataset is too large for the algorithm. I wonder anyone can help me point out what's making the following algorithm too slow? Thanks a lot!

    """
    class TreeNode(object):
    def init(self, x):
    self.val = x
    self.left = None
    self.right = None

    class Solution(object):
    def rob(self, root):

        def robHelper(root, flag): 
            if not root: 
                return 0
            if flag == 1:
                total = root.val + robHelper(root.left, 0) + robHelper(root.right, 0)
            elif flag == 0:
                total = max(robHelper(root.left, 0), robHelper(root.left, 1)) + max(robHelper(root.right, 0), robHelper(root.right, 1))
            return total
        
        return max(robHelper(root, 0), robHelper(root, 1))
    

    """


  • 0

    consider a case: [3,2,3,null,3,null,1,null,null,4]
    0_1503754045052_08322936-23cb-4137-b893-d02f1782aca9-image.png


Log in to reply
 

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