OJ producing different answer than local machine


  • 0
    M

    The below code produces output 1 for a tree with just the node -3. It produces -3 when as intended when I run it on my system or hand trace it. Kindly let me know what I am missing here

    # Definition for a  binary tree node
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution:
        # @param root, a tree node
        # @return an integer
        maxsum = -2147483648
        
        def maxPathSum(self, root):
            self.findMax(root)
            return Solution.maxsum
        
        def findMax(self,current):
            if current == None:
                return 0
            left = self.findMax(current.left)
            right = self.findMax(current.right)
            Solution.maxsum = max(Solution.maxsum,left+right+current.val)
            ret = max(left,right) + current.val
            return ret if ret > 0 else 0

  • 0
    M

    I'm having the same problem in Java version. Basically, I have a static variable maxsum which is the same as in your code. I have no idea why it works on my Eclipse but gets wrong answer on OJ. Can someone help?


Log in to reply
 

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