weird errors python solution: suspect leetcode issue


  • 0
    F

    The code cannot even pass
    Input:
    [0]
    Output:
    []
    Expected:
    [0]

    but if I use "run code":

    Your input

    [0]
    Your stdout

    0 1
    Your answer

    [0]
    Expected answer

    [0]
    Show Diff

    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    idx = 0
    class Codec:
    
        def serialize(self, root):
            """Encodes a tree to a single string.
            
            :type root: TreeNode
            :rtype: str
            """
            if not root: return ""
            stk = [root]
            ret = []
            while stk:
                cur = stk.pop()
                ret.append(str(cur.val))
                if cur.right:
                    stk.append(cur.right)
                
                if cur.left:
                    stk.append(cur.left)
            return ','.join(ret)
    
        def deserialize(self, data):
            """Decodes your encoded data to tree.
            
            :type data: str
            :rtype: TreeNode
            """
            
            def helper(data, minv, maxv):
                global idx
                if idx>=len(data) or data[idx]<minv or data[idx]>maxv:
                    return None
    
                root = TreeNode(int(data[idx]))
                idx+=1
                root.left = helper(data, minv, root.val)
                root.right = helper(data, root.val, maxv)
                print root.val, idx
                return root
            
            if not data:
                return None
            data = data.split(',')
            data = [int(d) for d in data]
            return helper(data, -sys.maxint-1, sys.maxint)
    
    # Your Codec object will be instantiated and called as such:
    # codec = Codec()
    # codec.deserialize(codec.serialize(root))
    

Log in to reply
 

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